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

J

Jake Lewis

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">
</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
 
R

RobG

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');
}
 
R

Richard Cornford

Jake Lewis wrote:
VE3DWindow = window.open('', 'VE3DWin',

When no URL is provided for the call to window.open the about:blank URL
is used (and/or variants).

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.
 
J

Jake Lewis

Richard Cornford said:
Jake Lewis wrote:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top