I have opened a new window with JavaScript:
var newwin = window.open('','preview','width=600,height=500');
Now I want to write some JavaScript to the window:
newwin.document.write("<script type='text/javascript'>alert('Hi!');<" + "/script>");
newwin.document.close();
However, the script never gets executed. Am I doing something wrong?
Update: Okay, now I got part of it working. Here is the current code I have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type='text/javascript'>
function SetContent()
{
$('#content').html('New Text');
}
function dowin()
{
var newwin = window.open('','preview','width=600,height=500');
newwin.document.write('<div id="content"></div><b>This should be replaced...</b>');
newwin.document.close();
var script = newwin.document.createElement("script");
script.type = "text/javascript";
script.src = 'jquery.js';
newwin.document.body.appendChild(script);
var script2 = newwin.document.createElement("script");
script2.type = "text/javascript";
script2.textContent = "(" + SetContent + ")();";
newwin.document.body.appendChild(script2);
}
</script>
</head>
<body>
<button onclick='dowin();'>Open</button>
</body>
</html>
It’s supposed to change the content of the div but as you can see, it doesn’t.
Your code running ok on my Firefox 3.7, Opera 10, and IE6, but you could try different approach by using DOM functions.
If its not working, you should double check popup blockers or javascript blockers on your browser.