Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery document ready function not working in IE

I have a function I'm using to replace some text output with a button or sold out label accordingly.

jQuery(document).ready(function() {
jQuery('td.register').each(function () {
    var text = jQuery(this).text();
    var exploded = text.split(',');
    console.log(exploded[0]);
    console.log(exploded[1]);
    if (exploded[0] == 0) {
        jQuery(this).html("<font color='red'>SOLD OUT</font>");
    } else {
        jQuery(this).html("<a class='button' title ='Register for this event' href='" + exploded[1] + "'>Register</a>"); 
    }
})
});

It seems to work fine on most browsers, but the client is complaining in IE9 it's not working. When I test it on my computer, most of the time it works, but sometimes it doesn't, and every time I test it on browsershots.org it doesn't work. It shows up in browsershots.org tests as if the jQuery didn't even run.

like image 557
Scott Fister Avatar asked Mar 13 '13 07:03

Scott Fister


People also ask

Where do I put jQuery document ready?

As a convention, placing the script element just before the closing body tag makes the script wait for the rest of the document to load before running. We also can make this process faster in jQuery by using the $(document). ready() method. The $(document).

Can I use jQuery without document ready?

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is document ready in jQuery?

Definition and Usage. The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

What is document ready and window load in jQuery?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.


1 Answers

console is not defined in IE9 modify your code like

jQuery(document).ready(function() {
jQuery('td.register').each(function () {
    var text = jQuery(this).text();
    var exploded = text.split(',');
    if(typeof(console)!='undefined'){
        console.log(exploded[0]);
        console.log(exploded[1]);
    }
    if (exploded[0] == 0) {
        jQuery(this).html("<font color='red'>SOLD OUT</font>");
    } else {
        jQuery(this).html("<a class='button' title ='Register for this event' href='" + exploded[1] + "'>Register</a>"); 
    }
})
});
like image 152
Dakait Avatar answered Oct 20 '22 04:10

Dakait