Can't load multiple document.write scripts

698 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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.