Tuan Nguyen
Tuan Nguyen

Reputation:

jquery .html does not work with script

This code is not working on IE8 at all. FF3 is executing but the page is blank and seems loading not ending.

My code is:

$("#leaderBoard").html("<script2 language=\"javascript2\"> document.write('<scr'+'ipt language=\"javascript21.1\">alert(1)</scri'+'pt>'); </script2>".replace(/script2/gi, "script"));

I want to let page load ad on ready.

Upvotes: 1

Views: 14748

Answers (6)

asnazrul
asnazrul

Reputation: 29

Try this approach:

//////////////////function in page's script section

function aFunction (x) {

    ////script you want to execute

    alert(x);

}

//////////////////////in page's body

var d = $("<div />");
d.ready(function(){aFunction("x");});

Upvotes: 1

Mihai Lazar
Mihai Lazar

Reputation: 2307

document.write()

does not work with the window.onload event

try this code

 $('.myDiv').html('<script src="path\/to-add\/provider"><\/script>');

somewhere near the end of your document Notice that you have to escape the '/' charecter

Upvotes: 0

roborourke
roborourke

Reputation: 12217

Why not try using jQuery's $.getScript(); function?

http://docs.jquery.com/Ajax/jQuery.getScript

Upvotes: 1

user37125
user37125

Reputation: 209

This works for me in FF3 and IE7:

$("#leaderBoard").html('<sc'+'ript language="javascript1.1">alert(1);</sc'+'ript>');

When you use document.write after the page has loaded, it replaces the entire document (http://javascript.about.com/library/blwrite.htm). You essentially replaced your page content with a script tag, causing it to appear blank.

Upvotes: 5

Tomalak
Tomalak

Reputation: 338148

When you actually have jQuery already, why all the obfuscating hassle?

Simply load the ad HTML into the leaderBoard object, and you're done.

$(document).ready( function() {
  $("#leaderBoard").load("/ad_generator.php");
});

Where ad_generator.php would produce an HTML fragment based on some randomization scheme.

Upvotes: 3

adam
adam

Reputation: 22587

try wrapping your code in

$(document).ready(function() {
 // your code here
});

Upvotes: 0

Related Questions