...

View Full Version : Nested document.writeln() question...



jamescover
07-01-2004, 01:42 PM
Hi:

I'm still wrestling with the same problem:

Is there any way to pass a variable in a nested document.writeln() statement???

Example Script:


<script>
<!--

myImg=new Image()
myImg.src="http://www.ekigroup.com/javascript/Clipboard.jpg";
myImg="clipboard";
function passVar(){
document.open();
document.writeln("Your image name is " +myImg);
document.writeln("<script>");
document.writeln("<!--");
document.writeln("function showDoc(){");
document.writeln("myImg=\"clipboard\";");
document.writeln("document.open();");
document.writeln("document.writeln(\"Your image name is \" +myImg);");
document.writeln("document.close();");
document.writeln("}");
document.writeln("setTimeout(\"showDoc()\",5000);");
document.writeln("//-->");
document.writeln("<\/script>");
document.close();
}

//-->
</script>

<body onload="passVar();">


The second document output:


Your image name is clipboard
<script>
<!--
function showDoc(){
myImg="clipboard";
document.open();
document.writeln("Your image name is " +myImg);
document.close();
}
setTimeout("showDoc()",5000);
//-->
</script>


The third document output:

Your image name is clipboard





The above script works, but as you can see, I have to redeclare myImg. If I don't, the second time the script writes to the page, I get a js error:myImg is undefined.

I thought this would be a session variable, for lack of a better term. So why can't I carry the value to the third document without redeclaring it??? Any ideas???



-james

glenngv
07-01-2004, 02:05 PM
document.writeln('document.writeln("Your image name is ' + myImg + '");');

I used single quote as delimiter for the outer document.writeln statement to avoid confusion.

But I don't know exactly what you really wanted to do with that script. I don't know why you're using nested document.writeln statements. Maybe there are better ways of doing it.

dumpfi
07-01-2004, 02:19 PM
<script>
<!--
myImg=new Image()
myImg.src="http://www.ekigroup.com/javascript/Clipboard.jpg";
myImg="clipboard";
function passVar(){
document.open();
document.writeln("Your image name is " +myImg);
document.writeln("<script>");
document.writeln("<!--");
document.writeln("function showDoc(){");
document.writeln("document.open();");
document.writeln("document.writeln('Your image name is '"+myImg+");");
document.writeln("document.close();");
document.writeln("}");
document.writeln("setTimeout('showDoc()',5000);");
document.writeln("//-->");
document.writeln("<\/script>");
document.close();
}
//-->
</script>


This should do it.

//Edit:

Hm.. I'm somehow too slow. :(

jamescover
07-01-2004, 02:36 PM
You're right, Glenn, there should be an easier way, but I still can't get it to work. For some reason, embedding a .js via writeln() still crashes IE. Oddly enough, it works fine offline.

For example, this works as expected offline:


<script>
<!--

function embedScript(){
document.open();
document.writeln("<script type=\"text\/javascript\" language=\"javascript\" src=\"go.js\"><\/script>");
document.close();
}

//-->
</script>

<body onload="javascript:embedScript();">


go.js:

function go(){
alert("hello");
}
go();


Anyway, I'm working with 3 documents, created from one script. The simpler way, I suppose, would be to store all variables in an external script, embed it in each document, and access them that way, instead of redeclaring them.
Unfortunately, I can't do such a simple little thing as that :-(



The good news is, your syntax worked!!! Thanks. I was escaping everything...never thought of inverting...




-james

jamescover
07-01-2004, 02:53 PM
Hey, dumpfi, thanks for your input. I appreciate it.

document.writeln("<script language='javascript' type='text/javascript' src='go.js'></script>");


Hmmm...I thought I had tried it this way before...it's working now. It's always the little things that take up so much tiime...


Thanks, again, Glenn!


-james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum