Forum Moderators: open

Message Too Old, No Replies

document.write troubles...HELP!

         

funwithme

12:29 am on Mar 26, 2004 (gmt 0)

10+ Year Member



I am trying to call a function (onLoad=trap()) when writing a web page via document.write. This function is defined in a copywrt.js file. Everything else works fine, but when including the onload a couple of different ways, this either generates an error or locks up (explorer 6)

function ncdisplay_image(imagename) {
PreView = window.open("", "Preview", "toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=0,resizable=0,copyhistory=0,width=670,height=500");
PreView.document.open();
PreView.document.write('<script type="text/javascript"language="javascript" src="http://www.lifescapephoto.com/copywrt.js">');
PreView.document.write('<\/script>');
PreView.document.write('<META HTTP-EQUIV=imagetoolbar CONTENT=no>');
PreView.document.write("<HTML><HEAD>");
PreView.document.write("<TITLE>LifeScape Photography</TITLE>");
PreView.document.write("</HEAD><BODY onLoad='trap()' BGCOLOR=333333 TEXT=000000>");
PreView.document.write("<CENTER>");
PreView.document.write("<IMG HSPACE=0 VSPACE=0 " +
"SRC='" + imagename + "'>");
PreView.document.write("</CENTER>");
PreView.document.write("</BODY></HTML>");
PreView.document.close();
}

Purple Martin

1:33 am on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're trying to rewrite the whole HTML. I think document.write() writes to the body of the document - so I don't think you should write "<HTML>" into the body.

Also, I think each line of code writes immediately - I don't think it gets stored until document.close() happens. So if I were you I'd build a string, then write the whole string at once.

funwithme

1:54 am on Mar 26, 2004 (gmt 0)

10+ Year Member



I use this to dynamically change an image with the same background etc, when a user clicks on a thumbnail. It opens another page and displays the image without menues, etc. It works until I try to inclue the Body Onload="trap()" - it is code to trap right clicks. I think my problem is a syntax problem with quotation marks in that line. I still don't understand the use of the single , double quote and how to unclude something that has quotes already - ie: onload="trap()"

Purple Martin

3:01 am on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The quotes in the relevant line in your fost post look fine.

Instead of rewriting the entire document, why don't you just change the innerHTML property of the body element? Then you won't have to worry about writing an onLoad attribute, it will already be there.

funwithme

3:47 am on Mar 26, 2004 (gmt 0)

10+ Year Member



can you eleborate on this...a small sample?

How can I take an existing page an write just the middle body elements?

Rambo Tribble

3:48 am on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, you can write the whole HTML document in a new window, in which case you should include the DOCTYPE declaration. I do notice that you have a script tag, a meta tag, and a tag I don't recognize (<\/script>) before the head of your document, when at least the first two should go inside.

I believe Purple Martin's suggestion of using innerHTML has merit, though it can probably also be done a little easier with an ID on the body element, then using getElementById to change the CSS background property.

Quotes is a convoluted issue but this is a basic example:
onclick="myFunc('var1')" is okay, never onclick="myFunc("var1")".
This is okay on a document write :
document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'
+'<html><head><title>Title stuff</title>'
+'<link rel="stylesheet" type="text/css" href="myCSS.css" media="print">')

To place quotes within a text string inside declaration escape them as in \' or \" (\ is the escape character).

Your line:
PreView.document.write("<IMG HSPACE=0 VSPACE=0 " +
"SRC='" + imagename + "'>");

might work better as "src=\"" + imagename + "\">");