0

I'm trying to load a multiple document.write scripts into a single document. Like this:

:DocWrite.js

document.write('<a href="#"><img src='myimage.gif'/></a>');

MyHTML.html

$('.myclass').each( function( index, element ){
    var script = unescape('%3Cscript src="'+DocWrite.js+'" type="text/javascript"%3E%3C/script%3E');
    $(element).html( script );

});
<div class="myclass">document.write loads image here</div>
<div class="myclass">stops here and hangs</div>
<div class="myclass">same</div>

The first div loads the script and the image is written inside but it stops after that. I would just change DocWrite.js but I don't have access to it. Any ideas how I can fix this? Thanks in advance.

3
  • Try using document.body.innerHTML+="content to add" instead of document.write() Commented Feb 10, 2011 at 2:44
  • I don't have access to DocWrite.js Commented Feb 10, 2011 at 2:46
  • Oh, then the only possibility I see is overwriting the document.write function, but I'm truly not sure if I should be recommending this as I have no idea if it would work or what the downsides are. Commented Feb 10, 2011 at 2:53

1 Answer 1

0

I think you're missing some quotes around DocWrite.js, try this instead:

$('.myclass').each( function( index, element ){
    var script = unescape('%3Cscript src="' + 'DocWrite.js' + '"type="text/javascript"%3E%3C/script%3E');
    $(element).html( script );
});

I just added single quotes around "DocWrite.js". Without the quotes, the JavaScript interpreter will think that DocWrite is an object and that you're trying to access the js property on that object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.