JQuery $(document).ready() and document.write()

picardo has the approach I would've used. To expand on the concept, take a read:

$('<script/>')
   .attr('src', 'http://myurl.com/page.aspx?id=1')
   .appendTo('body');

Alternate style:

var imgnode = $('<img alt="Second image for id 1"/>')
   .attr('src', "image1.jpg");

$('#id1').append(imgnode);

Be sure to use the attr method to set any dynamic attributes. No need to escape special symbols that way.

Also, I'm not sure what the effectiveness of dynamically generating script tags; I never tried it. Though, it's expected that they contain or reference client-side script. My assumption is that what page.aspx will return. Your question is a little vague about what you're trying to do there.


With the requirements given, no, you can't use document.write without really hosing up the document. If you're really bent on not changing the code, you can override the functionality of document.write() like so and tack on the result later:

var phbRequirement = "";

$(function() {
  document.write = function(evil) {
    phbRequirement += evil;
  }
  document.write("Haha, you can't change my code!");
  $('body').append(phbRequirement);

});

Make sure you overwrite the document.write function before it is used. You can do it at anytime.

The other answers are boring, this is fun, but very pretty much doing it the wrong way for the sake of fulfilling the requirements given.