468,533 Members | 1,922 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,533 developers. It's quick & easy.

Cant correctly load .js files when creating page dynamically in IE

I have an HTML page that loads fine including the .js file

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript" src="ve3d.js"
</script>

</head>
<body>
.......
.......
.......
</body>
</html>

but when I try and load a window and create the same text dynamically
the .js file reports syntax errors at arbitrary points ( eg closing
brackets )and the scripts cant be called from the body of the page.

The code Im using to generate the page follows.
Its just the above code with a .document.write before each line. As
you can see Ive split the </script> to </scr'+'ipt>' tag so thats not
the problem.

VE3DWindow = window.open('', 'VE3DWin',
'resizable=yes,width=400,height=400')
VE3DWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
4.01 Transitional//EN"')
VE3DWindow.document.write('"http://www.w3.org/TR/html4/loose.dtd">')
VE3DWindow.document.write('<html>')
VE3DWindow.document.write('<head>')
VE3DWindow.document.write('<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">')
VE3DWindow.document.write('<title>Untitled Document</title>')
VE3DWindow.document.write('<script language="JavaScript"
type="text/JavaScript" src="ve3d.js"></scr'+'ipt>')
VE3DWindow.document.write('</head>')
VE3DWindow.document.write('<body>')
.........
.........
.........
VE3DWindow.document.write('</body>')
VE3DWindow.document.write('</html>')
VE3DWindow.document.close()

Everything works fine in NN6.2 but I'm getting random syntax errors in
the js script in IE6.

It's the content of the ....... section that is the reason to create
the thing dynamically.
Any ideas appreciated, Jake
Jul 23 '05 #1
4 2443
Jake Lewis wrote:
[snip]

but when I try and load a window and create the same text dynamically
the .js file reports syntax errors at arbitrary points ( eg closing
brackets )and the scripts cant be called from the body of the page.

[snip]

I don't know what's wrong with your code, but there was a
discussion recently on the best way to write slabs of HTML
to a page. The consensus was to write your HTML to an
array, they use join() to concatenate it and write it to
the page. Some code is below.

Note that if you use an array, it's very easy to handle
long lines of text. This seems much easier than

VE3DWindow.document.write('<html>')

which is replaced by:

"<html>",

The reason I've used a button to create the page is because
using onload or similar will not work if popup blocking is
enabled - the browser will only open a new window in
response to a user action, not automatically.

<html>
<head>
<title>New Page</title>
<script type="text/javascript">
function loadWindow() {
var VE3DWindow = window.open('',
'VE3DWin','resizable=1,width=400,height=400')
var a = [
'<!DOCTYPE HTML PUBLIC ',
'"-//W3C//DTD HTML 4.01 Transitional//EN"',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html>',
'<head>',
'<meta http-equiv="Content-Type" ',
'content="text/html; charset=iso-8859-1">',
'<title>Untitled Document</title>',
'<script type="text/JavaScript" src="xx.js"></scr',
'ipt>',
'<script type="text/JavaScript">alert(location);</scr',
'ipt>',
'</head>',
'<body>',
'<p>Here is a page</p><p>here is some text</p>',
'<p>and some more text</p>',
'<form action=""><input type="button" ',
'value="Open Window"',
'onclick="xx();"></form>',
'</body>',
'</html>',
];
VE3DWindow.document.write(a.join(""))
VE3DWindow.document.close()
}
</script>
</head>
<body>
<form action="">
<input type="button" value="Open Window"
onclick="loadWindow();">
</form>
</body>
</html>
file xx.js:

function xx() {
alert('hi');
}
Jul 23 '05 #2
Jake Lewis wrote:
<snip>
VE3DWindow = window.open('', 'VE3DWin',
When no URL is provided for the call to window.open the about:blank URL
is used (and/or variants).

<snip> VE3DWindow.document.write('<script language="JavaScript"
type="text/JavaScript" src="ve3d.js"></scr'+'ipt>')

<snip> ^^^^^^^

This is a relative URL; relative to what exactly? If it is relative to
about:blank then the source loaded will be some sort of error
page/report, and that will be interpreted as a series of javascript
syntax errors.

As I recall (and it has been a long time since I have written anything
using pop-ups) you can get round the problem by:-

1. loading a small page from your own site into the new
window (which might produce timing issues if the document
has not finished loading when you start writing to it.
Avoidable by having the loaded page call-back into its
- opener - to signal that it is ready to be written to).

2. Write a BASE element into the HTML source using the
location.href property of the current page, so that URLs
employed in the written HTML will be relative to it.

Richard.
Jul 23 '05 #3
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message news:<ck*******************@news.demon.co.uk>...
Jake Lewis wrote:
<snip>
VE3DWindow = window.open('', 'VE3DWin',
When no URL is provided for the call to window.open the about:blank URL
is used (and/or variants).


2. Write a BASE element into the HTML source using the
location.href property of the current page, so that URLs
employed in the written HTML will be relative to it.

Richard.


Thanks for the tip here - this seems to be the way to do it. The fact
that IE was producing a script error and bringing up the .js file led
me to believe it had found it correctly, but clearly it hadnt.

Cheers Jake
Jul 23 '05 #4
thanks, I was sure there was a more elegant way to do that. Best Wishes, Jake
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by KnotKnormal | last post: by
9 posts views Thread by netasp | last post: by
6 posts views Thread by Steve Hershoff | last post: by
1 post views Thread by =?Utf-8?B?cHJhYmh1XzI3Xzg0?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.