Jquery.html not working with script

This code doesn't work at all on IE8. FF3 runs, but the page is blank and it seems that the download is not ending.

My code:

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

      

I want the page load declaration to be ready.

+1


source to share


6 answers


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 have essentially replaced the content of your page with a script tag, which makes it empty.

+5


source


When you already have jQuery, why all the confusing problems?

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



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

      

Where ad_generator.php

will create an HTML snippet based on some randomization scheme.

+3


source


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

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

+1


source


Try this approach:

//////////////////function in page script section

function aFunction (x) {

    ////script you want to execute

    alert(x);

}

//////////////////////in page body

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

      

+1


source


try wrapping your code in

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

      

0


source


document.write()

      

doesn't work with window.onload event

try this code

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

      

somewhere near the end of your document Note that you need to avoid the "/" charecter

0


source







All Articles