tags:

views:

250

answers:

3

I am trying to use jQuery.Load to load an ad call that has a document.write, and for some reason its not able to, or in firefox atleast, reloads the page with the entire ad.

Here is the simplified version of the code.

DynamicLoad.html

<html>
<head>
<script src="http://www.prweekus.com/js/scripts.js?3729212881" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Load of Script</title>
<script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">
   google.load("jquery", "1.3.2");
</script>
<script type="text/javascript">
   $(document).ready(function(){
    $("#myButton").click(function() {
        $("#myDiv").load("source.html");
    });
   });
</script>
</head>
<body>
<button id="myButton">Click Me</button>
<div id="myDiv"></div>

<div id="slideAdUnit"></div>

</body>
</html>

Source.html

<script language="javascript" type="text/javascript">
  document.write('<script language="javascript" type="text/javascript"><\/script>');
</script>
test

Once you click the button in FF the browser just waits for something to load. Any thoughts ?

Eventually I would be passing a src element in the document.write which points to our ad server.

Thanks for your help.

+3  A: 

You can't use document.write after the page has finished loading: it will replace the contents of the page if you do.

The easiest way to load a script dynamically with jQuery is:

$.getScript( url );

Another way is to create a new <script> element and add it to the document.

interjay
A: 

You need to insert a script block into the DOM. You can't just write it into the document like that.

Use: $(document.createElement('script'));, then set the attributes you require.

var newBlock = $(document.createElement('script'));
$(newBlock).attr('src','http://...')
Diodeus
A: 

writeCapture.js (full disclosure: I'm the author) can solve this:

$("#myDiv").writeCapture().load("source.html");
noah