Access a function in js [duplicate] - javascript

This question already has answers here:
Can we call the function written in one JavaScript in another JS file?
(10 answers)
Closed 8 years ago.
I have a function in my separate js file called hello().
On my page I have this js:
<script type='text/javascript'>//do hello fnc</script>
How can I get the on page script to call the function in my main js file?

Call it in the src attribute of another script tag, before referring to any of its functions in this script.
<script type='text/javascript' src='main.js'></script>
<script type='text/javascript'>
hello();
// Anything defined in previously included js files like main.js
// is now available, as well as anything defined on this page.
</script>

Load the external file first and call the function:
<script src=external.js></script>
<script>
hello();
</script>

You just need to include the external js file:
<script type="text/javascript" src="MyOtherFile.js"></script>
And then wherever in you code you want to call the funciton:
<script type="text/javascript">hello();</script>

Related

(jQuery) Uncaught ReferenceError: $ is not defined in Laravel [duplicate]

This question already has answers here:
JQuery - $ is not defined
(36 answers)
Closed 6 years ago.
This post was edited and submitted for review 9 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Jquery
<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
my code
<script type="text/javascript">
function readURL(input){
if(input.files && input.files[0]){
var reader = new FileReader();
reader.onload=function(e){
$('#showimages').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#inputimages").change(function(){
readURL(this);
});
</script>
<img src="" id="showimages">
<input type="file" name="images" id="inputimages">
and i got this error
Uncaught ReferenceError: $ is not defined
at http://localhost/project/public/user/1/edit:308:9
*line 308 -> $("#inputimages").change(function(){
Im new in js and jquery, Help me solve this error please...
I wrote the script out of the section. That was why the error occurred, see my answer.
Thankyou for everyone!
This error is solved, in .blade.php laravel i have to write
#section('script')
<script type="text/javascript">
//my code here
</script>
#endsection
To use Jquery, remember it has to import before using
Your problem is look like your Jquery is not imported. Maybe your path is incorrect or you imported it after your script.
First, check this url http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js with with your web browser. If it exist you will see a lot of javascript code there.
Then to import it from your local server.
<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script>
//your script here.
</script>
Another choice using cdn instead:
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
//your script here
</script>
Looks like jQuery wasn't loaded when your script is exectued.
You should load jQuery before scripts that are using it.
You have JavaScript running before the page is fully loaded.
You can load another plugin which may have overwritten the $ variable.
Your JavaScript file is not being properly loaded into your page(Check you script path and try to open link directly on browser).
Make sure all javascript code is being run inside a code block such as:
$(document).ready(function () {
//your code
});
or
(function($){ })(jQuery);
or equivalent pure javascript code.
There may be two potential problem in your code.
You don't have to put your code before scripts that using it. (Check your view source from browser)
You may imported another jQuery library.
If you imported two different jQuery Library, you can solve with noConflict()
e.g
<script type="text/javascript">
j = $.noConflict();
function readURL(input){
if(input.files && input.files[0]){
var reader = new FileReader();
reader.onload=function(e){
j('#showimages').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
j("#inputimages").change(function(){
readURL(this);
});
</script>
Try my updated answer. Assumed you have main layout file.
<head>
<script src="{!! asset('plugins/jQuery/jquery-2.2.3.min.js') !!}"></script>
<script src="{!! asset('bootstrap/js/bootstrap.min.js') !!}"></script>
<script src="{!! asset('plugins/select2/select2.full.min.js') !!}"> </script>
<script src="{!! asset('plugins/slimScroll/jquery.slimscroll.min.js') !!}"></script>
<script src="{!! asset('plugins/fastclick/fastclick.js') !!}"></script>
#yield('script')
<body>
#yield('content')
<script src="{!!asset('plugins/datatables/jquery.dataTables.min.js')!!}"></script>
</body>
</head>

Javascript External Object Global? [duplicate]

This question already has answers here:
JavaScript Code Inside <script> Tag
(3 answers)
Closed 8 years ago.
I'm messing around with objects and methods, and I have this really simple example that I use to test with:
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
shout.hello("World");
And this works fine. However, if I place the object shout in an external file, and then run shout.hello("world"); I get nothing:
//external file: test.js
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
<!-- my html document -->
<script src="test.js">
shout.hello("World");
</script>
What am I doing wrong?
From MDN:
script elements with an src attribute specified should not have a script embedded within its tags.
You need two separate script tags, one to import your external script, and another to call the function, for example:
<script src="test.js"></script>
<script>
shout.hello("World");
</script>
You need two separate script tags, the content of a tag with src attribute is ignored.
<script src="test.js"></script>
<script>
shout.hello("World");
</script>

How i can call a function from external javascript file in another external javascript file?

I have 2 external javaScript files. I want to call a function of first file into second file.
any body can help me?
How i can call a function from external javascript file in another external javascript file?
JS prints the values on DOM, so once the page has been rendered on the browser, it is all treated as one large document. This means, after parsing, you can call any function from any JS file ONLY IF that function has been loaded on DOM before your calling function!
That explained, assuming you have 2 files js1.js and js2.js,having fn1() in js1 and fn2() in js2
So if you have to call fn1() in js1.js, load it before js2.js, once loaded simply make a call to fn1() from js2.js and it would work!!
js1.js
function fn1(){
// some code
}
js2.js
function fn2(){
fn1(); //function call to another file.
}
Just ensure the order of loading :
<script src="js1.js" type="script/javascript"> <!-- Load the main program JS first -->
<script src="js2.js" type="script/javascript"> <!--Load the calling program JS after it -->
One additional way of doing it
<script src="js1.js" type="script/javascript"> //Load the main program JS first
<script language="javascript">
fn1(); //call the function directly from the HTML embedded JS :)
</script>
import both js file into a single html page, and then any function you want to call from any where you want
html
<html>
//
<script type="text/javascript" src="1st.js"></script>
<script type="text/javascript" src="2nd.js"></script>
//
</html>
1st.js
function js1(){
//...
}
2nd.js
function js2(){
js1();
}
You have to make sure that script from which you are calling your
function is downloaded after the script in which you have your
function;
And, of course, that the place from where you are calling you function has scope access to the function you call.

Extracting JavaScript into separate file (from HTML). Duplicate call to function needed

See the source of http://marakana.com/s/post/1096/samples/try6.htm
It defines a function and calls it on load of document. (Which is the final step of this tutorial)
I tried to put it into a seperate JS file.
Runs correctly only if I call onload both in JS and in HTML.
But not only body onload or only from JS. I guess I am doing something wrong.
So, following works:
<head>
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"/>
<script type="text/javascript">
window.onload = function () {
makeWYSIWYG(document.getElementById('editor'));
};
</script>
</head>
<body onload="makeWYSIWYG(document.getElementById('editor'));">
Why do I need to call the function twice?
I only have the function definition in "Scripts/makeWYSIWYG.js"
function makeWYSIWYG(editor) {
...
return editor;
};
Thanks,
There are no reason to call the function twice. The is enough.
With the first window.onload you could be changing a former function callback assignment (i.e. in a imported script).
The problem was actually the closing tag, "/>", here:
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"/>
I should have written:
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"> </script>
I guess the second script was helping the tag to be closed and making it run...
More info here: Why don't self-closing script tags work?

Is it true that first line of Javascript will be executed only after all script tags with src attribute are downloaded and evaluated?

By first line of Javascript I meant the JS code inside tags with no "src" attribute.
By right it will be evaluated top down.
Read more about it from my previous answer: Load and execution sequence of a web page?
Let me explain here:
<script type="text/javascript">
alert(jQuery); // alerts jQuery's namespace
</script>
<script type="text/javascript" src="jquery.js"></script>
The result is alerting undefined.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
alert(jQuery); // alerts jQuery's namespace
</script>
The result is alerting a object/function where defined.
The scripts are executed in the order they are listed in the html. It does not matter if they are inline JavaScript (within <script> tags) or if loaded as external scripts (<script src="xx.js">).

Categories

Resources