Reputation: 58
I am starting in web desing, and I have a problem with a javascript function in external file: My function javascript is like this:
function log()
{
alert("something");
$('#content').load('mod/lo.php');
}
And I call this function in html like this:
<script>log()</script>
and the script only shows the alert, the function load is ignored. But if I call the function like this:
<script>$(log);</script>
It works fine, the alert is showed and the load works.
What's the difference of these forms of calls?
I have search about this, but almost all the pages say that to call a function only put <script>function();</script>
Can anyone explain me the diferrence of this forms of call? there are problem when parameters are used?
Thanks and sorry for my bad English.
Upvotes: 1
Views: 111
Reputation: 734
The difference is, when you call <script>$(log);</script>
you're actually calling $(document).ready(log() {});
and Jquery executes its code. In the case of <script>log()</script>
the page isn't already loaded and doesn't execute the jquery code.
Upvotes: 1
Reputation: 34489
$
syntax is a jQuery shorthand, what it actually means is call this function when the document has loaded. It's exactly the same as writing:
$(document).ready(function() {
alert("something");
$('#content').load('mod/lo.php');
});
This probably indicates that the DOM item with the ID content
doesn't yet exist when the script executes. The affect of this is the jQuery selector $('#content')
will return 0, which prevents load()
ever being called. Once the document has fully loaded however it will be present and your load()
will execute.
Upvotes: 8