Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<script> inside javascript code document.write()

I got a portion of javascript code embedded in HTML (generated on the server side) that looks like this:

function winWriteMail2(){
  var win = open('','wininfo', 'width=400,height=300,scrollbars=yes,resizable=yes');
  win.document.open();
  win.document.write('<HTML><HEAD><META http-equiv="Content-type" content="text/html; charset=iso-8859-2"><LINK rel="stylesheet" type="text/css" href="/css/main.css">');
  win.document.write('<scr' + 'ipt language="javascript" type="text/javascript" src="/js/JSFILE.js"></scr' + 'ipt>');
  win.document.write('</HEAD><BODY BGCOLOR="#f7f3e7">');
  <!-- window content goes here -->
  win.document.write('</BODY></HTML>');
  win.document.close();
}

This code gets executed on click of a element.

The problematic part for me is the inclusion of javascript file - it works ok in Firefox and Chrome, but IE (7 and 8, as I tested) behaves strange. With the line containing JSFILE there, the window on click gets opened, but is empty, CPU is 100% busy and only way is to kill IE.

Anyone can help with handling this problem? Maybe I should use some other way to insert the javascript files in there?

I tried, instead of win.document.write(), the DOM-manipulation method, putting this part of code after win.document.close():

h = win.document.getElementsByName('head')[0];
js = document.createElement('script');
js.src = '/js/JSFILE.js';
h.appendChild(js);

but then the code isn't loaded, even in Firefox (and inspecting with firebug doesn't show it even can see it).


After some checks, I found out that the problem is caused by <script> elements with a src= attribute defined. If I add an inline script, like:

<script type='text/javascript'>alert('foo')</script>

within my document.write(), the window opens, the alert box shows up and everything's all right.

But using a

<script type='text/javascript' src='/js/foo.js'></script>

IE stalls when opening the new window, keeps using 100% of CPU.

like image 253
kender Avatar asked Dec 31 '09 13:12

kender


People also ask

What is write in document write in JavaScript?

The write() method in HTML is used to write some content or JavaScript code in a Document. This method is mostly used for testing purpose. It is used to delete all the content from the HTML document and inserts the new content. It is also used to give the additional text to an output which is open by the document.

What does document write () function do in JavaScript?

The document. write() method writes a string of text to a document stream opened by document.

What happens when you put document write () in script tags in the head of your web page?

The code within the script tag is executed when the browser processes the tag. The document. write() method is used to delete all the existing content from the HTML document and inserts the new content specified within document.

Can you write scripts in JavaScript?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code.


1 Answers

This code worked for me:

function winWriteMail2(){
    var win = open('','wininfo', 'width=400,height=300,scrollbars=yes,resizable=yes');
    win.document.open();
    win.document.write('<HTML><HEAD><META http-equiv="Content-type" content="text/html; charset=iso-8859-2"><LINK rel="stylesheet" type="text/css" href="/css/main.css">');
    win.document.write('</HEAD><BODY BGCOLOR="#f7f3e7">');
    win.document.write('this is the body content');
    win.document.write('</BODY></HTML>');
    win.document.close();

    var h = win.document.getElementsByTagName("head")[0];
    var js = win.document.createElement("script");
    js.type = "text/javascript";
    js.src = "js/scriptfile.js";
    h.appendChild(js);
}

Here is what I needed to change in your code to make it work:

//From
var js = document.createElement("script");
//To
var js = win.document.createElement("script");

You need to create the script element in the same document that you are appending.

like image 89
Gabriel McAdams Avatar answered Sep 28 '22 20:09

Gabriel McAdams