views:

81

answers:

2

Hi, I have some code like

var windowObject = window.open('','windowObject','arguments...');
windowObject.document.write("<html><body onload="alert(1);window.print();alert(2);"><div>some html</div></body></html>");

The problem is that everything works except the window.print event (on ie, on firefox, it's working).

Is there a workaround?

Thanks in advance, Gaurav

+1  A: 

It's a quotes issue: the double quote after onload= ends the string being written to the document. Change the onload quotes to single quotes. You also need to add a call to the close() method of the document:

var windowObject = window.open('','windowObject','arguments...');
windowObject.document.write("<html><body onload='alert(1);window.print();alert(2);'><div>some html</div></body></html>");
windowObject.document.close();
Tim Down
Hi thanks but still not working. Now my code is like below
gaurav
Ah yes, you need to call `windowObject.document.close()`. I've updated my answer.
Tim Down
A: 

In this line, it appears that you are trying to use nested double quotes:

windowObject.document.write("<html><body onload="alert(1);window.print();alert(2);"><div>some html</div></body></html>");

You more likely want to do:

windowObject.document.write('<html><body onload="alert(1);window.print();alert(2);"><div>some html</div></body></html>');
Brian Campbell