views:

695

answers:

4

This code giving error in IE

Internet Explorer Cannot Open the Internet Site Operation Aborted, how to fix this error?

var tip = "<p>Most computers will open PDF documents ";
tip += "automatically, but you may";
tip += "need to download <a title='Link to Adobe website-opens in a new window'";
tip +=" href='http://www.adobe.com/products/acrobat/readstep2.html'  
               target='_blank'>Adobe Reader</a>.</p>";

$(document).ready(function(){

    //IF NUMBER OF PDF LINKS IS MORE THAN ZERO INSIDE DIV WITH ID maincontent
    //THEN THIS WILL PUT TIP PARAGRAPH AS LAST CHILD OF DIV
    if($("div#maincontent a[href*='/pdf']").length>0){
    $("div#maincontent").children(":last-child").after(tip);
    }
});

See this page in IE : http://jsbin.com/oliho4

A: 

Is it the infamous alert that returns a blank page in IE?

It's a known and documented IE issue.

This error usually occurs when you're trying to modify a DOM element before the page finished loading.

Try moving your script at the bottom of the page.

Since you're using document ready, I don't think this causes the problem. Maybe is it because of another script on your page?

EDIT:

You can also try to fire your function on the body's onload attribute. I just read this here.

EDIT 2: I didn't see your link. Jitter is right, you're not using jQuery's $(document).ready()

Guillaume Flandre
+3  A: 

To me it seems obvious that you try to modify some element before the page has finished loading. At least that's exactly what you do on your demo page. You don't wrap the code in $(document).ready() as you did in the question

Try this demo-site instead http://jsbin.com/ivuqa which correctly wraps the relevant lines in ready()


Additionally there might be some problems when using XHTML. In that case just wrap the offending javascript part like this. (CDATA to satisfy the XML validation, javascript multiline comment to hide the the cdata from browser which don't understand it and thus would fail to run the javascript.

/* <![CDATA[ */
    var tip = "<p>Most computers will open PDF documents automatically, but you may need to download <a title='Link to Adobe website-opens in a new window'";
    tip +=" href='http://www.adobe.com/products/acrobat/readstep2.html' target='_blank'>Adobe Reader</a>.</p>";
/* ]]> */
jitter
metal-gear-solid
Check expanded answer
jitter
OK. and i think if i attach this code via external js then also validation problem can be solved.
metal-gear-solid
metal-gear-solid
@jitter - I neverunderstood properlyuse of CDATA but from my this question and ur answer egading validation Now i know understand what is the use of CDATA hanks
metal-gear-solid
A: 

Please use below to solve this issue:

/*code goes here*/

Add "defer='defer'" in your peice of javascript and issue get resolved. Please try.

Thanks

Saurabh Shrivastava
A: 

Script on this page may be useful: http://blogs.msdn.com/ie/archive/2009/09/03/preventing-operation-aborted-scenarios.aspx

Robert.K