Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch - JavaScript

Tags:

javascript

I'm running into a problem when trying to use the following code. It runs perfectly in Firefox, Safari, Chrome, and IE 9+10, but causes the rest of my page to 'crash' when running in IE8. The console error I receive is 'type mismatch' and the debugger points to the IF statement line. I've been banging my head against the wall trying to figure it out, but to no avail. Does anyone have a clue as to what the heck is going on here?

function writeIframe11092()
{
    alert("BEFORE");
    document.write('<iframe style=\"position:absolute;left:-40000px;\" src=\"https://mydomain.com/images/close.gif\" ></iframe>');
    alert("AFTER");
}

if (window.attachEvent) 
    window.attachEvent('onload', writeIframe11092() );
else if (window.addEventListener) 
    window.addEventListener('load',  writeIframe11092(),false);
like image 812
Minja Avatar asked Mar 20 '13 21:03

Minja


1 Answers

writeIframe11092() calls the function and returns a value (undefined in this case), and passes that value to attachEvent.

writeIframe11092 is a variable representing the function itself, that's what you want to use here.

if (window.attachEvent){
    window.attachEvent('onload', writeIframe11092);
}
else if (window.addEventListener){
    window.addEventListener('load', writeIframe11092, false);
}
like image 199
Rocket Hazmat Avatar answered Oct 18 '22 22:10

Rocket Hazmat