Forum Moderators: open
var w = window.open("", "newPopup", "resizable=yes, height=100, width=100, menubar=yes; status=yes");
txt = "<html><header>\n<title>M</title>\n</header>\n<body>\n" +
"<script src=\"a.js\"></script>\n" +
"\n<script>myAlert();</script></body></html>"
w.document.write(txt);
w.document.close();
And the a.js has function myAlert:
function myAlert() {
alert("salam");
}
What shall I do?
The standard practice is to split the closing tags - and I have escaped the f-slashes for good measure.
Another tip included, especially as you are compiling 'txt', is a valid use of the javascript: protocol - replacing the blank url with the 'javascript:' followed by the string. This avoids referencing the document before it exists (a risk that's occasionally mentioned).
[pre]
txt = "" +
"<html><header>\n<title>M</title>\n</header>\n<body>\n" +
"<script src=\"a.js\"><\/scr"+"ipt>\n" +
"\n<script>myAlert();<\/scr"+"ipt></body></html>";var w = window.open(
javascript: txt,
"newPopup",
"resizable=yes, height=100, width=100, menubar=yes, status=yes"
);
[/pre]
(there was also a ; that should be a ,)
I went back to the old doc.write. Still I couldn't get it to work without error, unless
myAlert is actually called within the linked script, rather than in the later script block. I guess this isn't what you're after. So the only solution I have is to wait for the pop to load: [pre]
txt = '' +
'<html><head><title>M</title>'+
'<script src="a.js"><\/scr'+'ipt>' +
'</head><body>' +
'<script>window.onload=function(){myAlert();};<\/scr'+'ipt>' +
'</body></html>';//
var D = window.open(
"","newPopup",
"resizable=yes,location=1, height=100, width=100, menubar=yes; status=yes"
).document;
//
D.open();
D.write(txt);
D.close();
[/pre]
PS.
<header> --> <head>