Forum Moderators: open

Message Too Old, No Replies

JS file include problem

Problem including .js file inside a poped up window with IE

         

Mohsen

12:50 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hi,
I'm trying to include a js file inside a pop up window. IE does not include JS file, bu Mozilla does.
My code is something like this:

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?

Bernard Marx

1:17 pm on Oct 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm guessing, but this is most commonly caused by the browser misinterpreting one or other of the closing </script> tags in the string as being actual HTML to be parsed immediately - thus prematurely closing the script block concerned.

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 ,)

Mohsen

1:40 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Thanks for the reply, but the problem still remains. Would you please test a sample html page which opens a popup window in which a js file is included.

Bernard Marx

2:26 pm on Oct 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right there. Some behaviour I've never seen before. It started revealing some internal workings. Maybe it was the script tags again.

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>