eozzy
eozzy

Reputation: 68680

jQuery not working - strange

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript">
    alert('works');
</script>
<script type="text/javascript">
    $(window).load(function () {
        alert('does not work');
    });
    ​
</script>

Very strange, I'm not sure why it isn't working.

Upvotes: 1

Views: 87

Answers (5)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038900

I think the function should be .ready() and not .load() (this sends an AJAX request):

$(window).ready(function () {

Also make sure you understand the distinction between $(document).ready and $(window).ready. The first will be triggered when the DOM is ready while the second when the DOM and all images are ready.

Upvotes: 5

Praveen Prasad
Praveen Prasad

Reputation: 32117

youe are doing window.load which waits for every thing to get loaded before executing. however you should use DOM ready function of jQuery.

jQuery(function(){
    //do something
});

Upvotes: 0

chigley
chigley

Reputation: 2592

Two options for what you're trying to achieve:

  1. $(function() { alert('works'); });

  2. $(document).ready(function() { alert('works'); });

It's up to you which one you use. Personally I prefer the shorthand code in option 1 but there's a readability advantage with the second option.

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272146

It should work after all images, css and scripts are loaded. Is there something that is taking too long to load?

Upvotes: 1

Bobby
Bobby

Reputation: 11576

Are you looking for this:

$(function() {
    alert("does work");
});

Upvotes: 0

Related Questions