Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Firstly, is there a way to use document.write() inside of JQuery's $(document).ready() method? If there is, please clue me in because that will resolve my issue.

Otherwise, I have someone's code that I'm supposed to make work with mine. The catch is that I am not allowed to alter his code in any way. The part that doesn't work looks something like this:

document.write('<script src="http://myurl.com/page.aspx?id=1"></script>');

The script tag is referencing an aspx page that does a series of tests and then spits out something like so:

document.write('<img src="/image/1.jpg" alt="Second image for id 1">')

The scripts are just examples of what is actually going on. The problem here is that I've got a document.write() in the initial script and a document.write() in the script that get's appended to the first script and I've got to somehow make this work within JQuery's $(document).ready() function, without changing his code.

I have no idea what to do. Help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
176 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...