Connecting Tech Pros Worldwide Help | Site Map

declare variables document.write()

  #1  
Old November 18th, 2008, 04:05 PM
Jean
Guest
 
Posts: n/a
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?
  #2  
Old November 18th, 2008, 05:15 PM
Henry
Guest
 
Posts: n/a

re: declare variables document.write()


On Nov 18, 4:00 pm, Jean wrote:
Quote:
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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dinamically Declare variable in ASP Michael answers 6 December 18th, 2006 03:55 PM
compare a large number of variables Einar answers 122 November 15th, 2005 03:33 AM
Posting a hidden field to a form in a document using javascript Sergio Otoya answers 8 July 23rd, 2005 04:59 PM
why don't you have to declare variables in javascript? Andy Fish answers 10 July 20th, 2005 10:46 AM