declare variables document.write()

J

Jean

function myFunction(){
document.write('var path="../images/2008/');
}
I use this kind of function to declare the variable path. It's on a
extern javascript file. If I use this function in an other function to
define var path, the result is an error: path not defined. Can someone
explain to me why this doesn't work?
 
H

Henry

function myFunction(){
document.write('var path="../images/2008/');}

I use this kind of function to declare the variable path.
It's on a extern javascript file. If I use this function
in an other function to define var path, the result is an
error: path not defined. Can someone explain to me why
this doesn't work?

Apart from being quite an odd thing to want to do, and probably
considerably more simply (and reliably) achieved by other means), -
document.write - inserts HTML contents into the HTML stream (assuming
the document is not already closed at the time) and in that context
your 'var path="../images/2008/' is just text content (not script
source code (to be the latter it would have to appear in a SCRIPT
element)), and even if that text were interpreted as script content it
would only result in a syntax error as the opening double quote at the
beginning of that appears to have been intended to be a string literal
is not matched with a closing double quote.
 
J

Jean

Apart from being quite an odd thing to want to do, and probably
considerably more simply (and reliably) achieved by other means), -
document.write - inserts HTML contents into the HTML stream (assuming
the document is not already closed at the time) and in that context
your 'var path="../images/2008/' is just text content (not script
source code (to be the latter it would have to appear in a SCRIPT
element)), and even if that text were interpreted as script content it
would only result in a syntax error as the opening double quote at the
beginning of that appears to have been intended to be a string literal
is not matched with a closing double quote.

Thanks for the reaction. The problem is solved. Like you said, there
are more simply (and reliably) methods to achief what I was trying to
do. The missing double quote was a typing error. The original script
had a closing double quote. http://www.decultuurconsument.be/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top