-2

I have some Jquery written that works fine in firefox and chrome but throws an ERROR in IE.

Here is the sections of code that when taken out allow the other parts of the script to be run correctly...(otherwise the entire file is not read by IE)

var product_qty_is_zero = ExternalCustom_product_qty_is_zero; //'import' external variable from view.phtml
var option_is_selected = false;
var options_exist = false;
if(  ($(".options-container-big").length > 0) || ($(".options-container-small").length > 0)  )
    options_exist = true;   

$('#main-button-btn-cart').mouseenter(function() {
        //@ controlling backorder message
        if(product_qty_is_zero)
        {
            $('#select-product-reminder').slideDown(300);
            $('#select-product-reminder').text("product is purchaseable but on backorder");
            $('#select-product-reminder').css("background-color", "#c3c3c3");
        }                                          
});

$('#main-button-btn-cart').click(function() {
    if(!option_is_selected && options_exist) 
        $('#select-product-reminder').slideDown(300);
    else
        productAddToCartForm.submit();
});



 $('#select-product-reminder').slideUp(300);
  option_is_selected = true;

 var image_info = ExternalCustom_image_info; //'import' variable from media.phtml
  var image_link;
  var $selected_value_variation = $('.selected').html();
  for (var i = 0; i < image_info.length; i++) 
  {
        if(image_info[i][0] == $selected_value_variation)
            image_link = image_info[i][1];
  }
  $('#main-image').attr("href", image_link);

If anyone could look over it and see if there are any errors that are common for IE to flag and give me some hints it would be greatly appreciated!

Note* I already tried the class and div extensions such as $('div.options-container-big') and $('div#main-button-btn-cart')

JohnFx
  • 34,542
  • 18
  • 104
  • 162
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49
  • 4
    What is the error that IE is throwing, and what are you doing to cause it? – AlienWebguy Jul 08 '11 at 21:41
  • 1
    Have you fired up a Javascript debugger to see where any exception might be getting thrown? – Suroot Jul 08 '11 at 21:45
  • 2
    Your browser preferences and judgements in the question are unnecessary. – NoAlias Jul 08 '11 at 21:46
  • IE says the error is here... – Nathaniel Wendt Jul 08 '11 at 21:47
  • @thepristinedesign: ***What*** error? And am I missing it, or is `if (console)` nowhere in the code you've quoted? *Edit*: Nope, just copied the entire question to a text editor and searched, "console" doesn't appear anywhere within it. – T.J. Crowder Jul 08 '11 at 21:48
  • 'console' is undefined. and yes, its nowhere near the code I placed that is why I am so confused. That error does not flag if I remove my lines of code. Reminder that this is only in IE so I figured it isn't an error in my code but rather an IE difference in reading the code – Nathaniel Wendt Jul 08 '11 at 21:50
  • IE doesn't have a native console. IE9 does but you have to turn it on I believe. IE Tester might as well, but it doesn't always work for me. – AlienWebguy Jul 08 '11 at 21:56
  • @Alien: IE8 and IE9 both have it, but only when the developer tools are open. When they aren't, it's not there. – T.J. Crowder Jul 08 '11 at 22:05

2 Answers2

0

Only thing I can see without more info that might cause some hiccups:

productAddToCartForm.submit();

If IE doesn't know what that is, it'll likely fail. Try wrapping it in JQuery-safe selector syntax:

$(productAddToCartForm).submit();

or:

// Assuming you have a <form id="productAddToCardForm"...>
$('#productAddToCartForm').submit();
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • The productAddToCartForm is a function call I believe to another external JS file. Hold on let me look for it – Nathaniel Wendt Jul 08 '11 at 21:53
  • You don't call functions that way. submit() is a function you append to a form object. – AlienWebguy Jul 08 '11 at 21:57
  • Good point. I would like to further apologize for my web inexperience. I had thought I saw a function that was similarily named in collection of files in the magento structure here. – Nathaniel Wendt Jul 08 '11 at 21:59
  • I found that I pulled productAddToCartForm.submit() from the onclick value of the submit button. I'm not sure where it gets the productAddToCartForm but this is probably the culprit here...I wonder how chrome and firefox can find it – Nathaniel Wendt Jul 08 '11 at 22:03
  • Hey everyone. I dont have enough reputation to post again but I resolved the issue, information continued at... http://stackoverflow.com/questions/6631431/javascript-jquery-commenting-causing-errors-in-ie – Nathaniel Wendt Jul 08 '11 at 23:06
0

You've said that IE says that console is undefined on the line

if (console) { ... // Error if `console` is not defined anywhere

That's quite correct. console is not a guaranteed built-in object on browsers, although many browsers do provide it. You probably want

if (typeof console !== "undefined") { ...

...instead. There we're not taking the value, we're just checking whether it exists.

This is an area of JavaScript that can get a bit confusing, but basically, if you try to take the value of an unqualified reference that really isn't declared anywhere, e.g.

if (foo) { ...

...it's an error. The reason this can get confusing is that you could assign to foo in the exact same situation, and it would be valid (it would be The Horror of Implicit Globals, but valid). Similarly, you can happily take the value of an object property that has never been defined:

var obj = {};
if (obj.foo) { ... // Not an error, we just don't go into the body of the `if`

...because that's not an unqualified reference. But with unqualified references, it's an error.

So why is console unqualified on IE8 and IE9, both of which have console.log and such? Because IE8 and IE9 don't have console [or console.log] unless you have the developer tools open. Strange but true. Wrong, but true. :-) Opening the developer tools, which are basically a plug-in for IE8 and IE9, adds the console object on-the-fly. (More on that in this other question (and answer) here on Stack Overflow.)

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for your reply this is helpful information. The only problem is that the code if(console) that throws the error is in the body of the page and the code I'm editing is in an external .js file. The error does not occur when I remove my lines of code but it somehow does occur when my lines of code (posted above) are included in the external .js file – Nathaniel Wendt Jul 08 '11 at 22:07
  • 1
    That probably means that something far away from your code that is called by your code is trying to output to the debug console. You should probably find that suspect code and fix it. Or you could patch the environment by adding this line of code before your code executes: `if (!window.console) {window.console = null;}` That will make it so "if (console)" doesn't generate an error. – jfriend00 Jul 08 '11 at 22:24
  • Sorry, the return key caused a premature posting (still fooled by no line breaks in comments). I've finished my previous comment now. – jfriend00 Jul 08 '11 at 22:27
  • Thanks all for the helpful comments! – Nathaniel Wendt Jul 08 '11 at 22:43