Script Error ?

J

Jürgen Heyn

Good afternoon,

when clicking an image I would like to open a new window to display a *.html
file.
The function "ShowInfo" should do this and replace the contents.
The script works fine (the new window opens and the desired code is
executed(displayed).
But the calling page returns: "Error on this page".
Microsoft Frontpage2000 returns: "This object does not support this property
or method."
What's wrong?
Thank you very much for any hint.
Best regards
Jürgen Heyn, Wilhelmshaven, Germany

<html>

<head>
<title>Title</title>
<base target="_self">
<script language="JavaScript">
<!-- Beginn

function ShowInfo()
{
var obj = window.open("Info.html", "Info" ,
"width=640,height=480,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=
no");
obj.clear();
obj.open();
obj.write('<html>');
...
obj.close();
}
// End -->
</script>

</head>
<body>
<a href="javascript:ShowInfo()"><img src="../Buttons/Button.gif" border="0"
width="36" height="36">
</body>
</html>
 
M

Michael Winter

Good afternoon,

when clicking an image I would like to open a new window to display a
*.html
file.
The function "ShowInfo" should do this and replace the contents.
The script works fine (the new window opens and the desired code is
executed(displayed).
But the calling page returns: "Error on this page".
Microsoft Frontpage2000 returns: "This object does not support this
property
or method."
What's wrong?

Simply? The object, obj, doesn't support those methods. :)
<html>
<head>
<title>Title</title>
<base target="_self">
<script language="JavaScript">

You are missing the required type attribute. The above should read:

<script type="text/javascript">

The language attribute is deprecated and not required.
<!-- Beginn

Don't use SGML comments in script blocks. That practice is now obsolete.
function ShowInfo()
{
var obj = window.open("Info.html", "Info" ,
"width=640,height=480,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=
no");
obj.clear();
obj.open();
obj.write('<html>');
...
obj.close();

The methods you are trying to use are part of the document object, not the
window object. As long as you always use the variable, obj, to access
document properties and methods, you could alter the window.open() call to:

var obj = window.open('Info.html','Info','...').document;

The window will be opened, but object will contain a reference to the
window's document, not the window itself.

You should also be aware that IE, and possibly other browsers, do not
support the clear() method. Really, you should not bother specifying a URL
and just use an empty string. This opens a blank window, which is what you
are trying to acheive anyway. If, for whatever reason, you do want to
specify a URL, check for support before calling the method:

if( obj.clear ) obj.clear();
}
// End -->

Again, forget SGML comments in scripts.
</script>

</head>
<body>
<a href="javascript:ShowInfo()"><img src="../Buttons/Button.gif"
border="0" width="36" height="36">

Read: http://www.jibbering.com/faq/#FAQ4_24

</body>
</html>

Mike
 
J

Jürgen Heyn

Hi Michael,

thank you very much for your quick response.
Your tipp: window.open("", "Info" ...).document works fine.
If I remove the <!-- and // --> from the script block the obj.write will be
written onto the actual page?
Further I would like to implement a button and a text to close the new
window. Here again my function:

function ShowInfo()
{
var obj = window.open("", "Info" ,
"width=640,height=480,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=
no").document;
obj.write('<html>');
obj.write('<head>');
obj.write('<title>Produkt Info</title>');
obj.write('</head>');
obj.write('<body>');
obj.write('<p><img src="../Pictures/Image.gif" border="0" width="100"
height="75"></p>');
obj.write('<div align="left">');
obj.write(' <table border="1" cellspacing="1">');
obj.write(' <tr>');
obj.write(' <td ><b>any text</b></td>');
obj.write(' </tr>');
obj.write(' <tr>');
obj.write(' <td ><b>any text</b></td>');
obj.write(' </tr>');
obj.write(' </table>');
obj.write('</div>');
obj.write('<p align="center">
obj.write('<a href="#" onClick="window.close()"><img
src="../Buttons/Close_Button.gif" border="0" width="36" height="36"><br>');
obj.write('<a href="#" onClick="window.close()"><font size="3"><b>Close
this window</b></a></font></p>');
obj.write('</body>');
obj.write('</html>');
}

Thank you very much in advance.
Best regards
Juergen Heyn, Wilhelmshaven, Germany
 
M

Michael Winter

If I remove the <!-- and // --> from the script block the obj.write
will be written onto the actual page?

Is that a question, or a statement? Removing the comment delimiters from
the script block should not cause the contents (that is, the code) to be
written on to the page. If it does, and your browser is
JavaScript-enabled, the browser is broken.
Further I would like to implement a button and a text to close the new
window. Here again my function:

I assume you mention that because what you've posted below doesn't close
the new window. If this is the case, it's probably because of the invalid
HTML used. Even if the close functionality does work, do read below so you
can identify, and correct, the erroneous code.
function ShowInfo()
{
var obj = window.open("", "Info" ,
"width=640,height=480,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=
no").document;
obj.write('<html>');
obj.write('<head>');
obj.write('<title>Produkt Info</title>');

I obviously forgot to point this out in my last post: when writing a
closing tag with document.write(), you must use:

obj.write('<startTag>...<\/endTag>');

Notice the backslash. Some browsers interpret the sequence "</" as the end
of a block, even if it isn't, in full, "</script>". The backslash prevents
this interpretation.

There are several instances in the code below where you will need to do
this.
obj.write('</head>');
obj.write('<body>');
obj.write('<p><img src="../Pictures/Image.gif" border="0"
width="100" height="75"></p>');
obj.write('<div align="left">');
obj.write(' <table border="1" cellspacing="1">');
obj.write(' <tr>');
obj.write(' <td ><b>any text</b></td>');
obj.write(' </tr>');
obj.write(' <tr>');
obj.write(' <td ><b>any text</b></td>');
obj.write(' </tr>');
obj.write(' </table>');
obj.write('</div>');
obj.write('<p align="center">
obj.write('<a href="#" onClick="window.close()">

Though this might not be an issue in practice, you should return false in
that event handler. This will prevent the browser from trying to change
the page when it should be closing it.

<a href="" onclick="window.close();return false">

You should use the same event code in the link below.

Note: href="" and href="#" are almost equivalent, though the former is
preferred.
<img src="../Buttons/Close_Button.gif" border="0" width="36"
height="36"><br>');

Notice here that you are missing the closing anchor tag. Insert <\/a>
before said:
obj.write('<a href="#" onClick="window.close()"><font
size="3"><b>Close
this window</b></a></font></p>');

You haven't nested the closing font tag properly. It should appear before
the closing anchor tag:

... said:
obj.write('</body>');
obj.write('</html>');
}

Hope that helps,
Mike

P.S. Apologies for the delay. I had troubles sending posts with both my
newsreader and news server.
 
J

Jürgen Heyn

Hi Michael,

thank you very much for help.
I used every tip to optimise my code. It works great.
Thanks again.
Regards
Jürgen Heyn, Wilhelmshaven, Germany
 

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,815
Messages
2,569,705
Members
45,494
Latest member
KandyFrank

Latest Threads

Top