www.webdeveloper.com
Results 1 to 2 of 2

Thread: My script won't work in Firefox - variable not defined

  1. #1
    Join Date
    Oct 2005
    Location
    West Chester, PA
    Posts
    244

    Arrow "Variable not defined" error when run in Firefox

    I am trying to write a script which returns a "Form Tree" for a page.
    basically, you type the address of a page into a textbox, then hit a button to open that page and set a reference to it. you then hit another button to display the form tree in the form of nested unordered lists.

    I am using 3 files currently (shown below):
    1. The main form of the script
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Form Search</title>
    <script language="javascript" src="formtree.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <form name="form1" method="post" action="">
    	<input type="text" name="txtURLtoCheck" id="URLtoCheck" value="formsearchtesting.htm" />
        <input name="cmdSubmit"  value="Open Window" type="button" onClick="open_win()" />
    	<input name="cmdGetTree"  value="Get Form Tree" type="button" onClick="formtree()" />
    </form>
    </body>
    </html>
    2. the code behind the script
    Code:
    //  GLOBAL VARIABLES //
    var win = ""
    
    function open_win() {
    	var url = document.forms[0].txtURLtoCheck.value;	
    	win = window.open(url);	
    }
    
    function formtree() {
    	/*
    	 *	Display the Names of all the forms on the page
    	 *	and all the elements thereof
    	 */
    	var id	
    	
    	document.write("<form>Form Tree for " + win.document.title + "<br />");
    	documen
    	document.write("<ul>");
    	for (i = 0; i < win.document.forms.length; i++) {				
    		document.write("<li>" + win.document.forms[i].name + "<ul>");
    		for (j = 0; j < win.document.forms[i].elements.length; j++) {
    			id = "chk_" + i + "_" + j;
    			document.write("<li>" + 
    				"<input type='checkbox' name='" + id + "' id='" + id + "'/>" +
    				win.document.forms[i].elements[j].name + "</li>");
    		}
    		document.write("</ul></li>");
    	}
    	document.write(	"</ul></form>" +
    				   	"End of Form Tree");
    }
    3. A page with forms to test it out on:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Testing Forms</title>
    <script language="javascript" src="formtree.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <hr />
    <p>a form</p>
    <form name="frm1" method="post" action="">
    	<p>type something here
    		<input name="txt_1" type="text" id="txt_1">
        </p>
    	<p>
    		<input name="chkCheckBox" type="checkbox" id="chkCheckBox" /> 
    	checkbox</p>
    	<p>
    		<select name="cboName" id="cboName">
    			<option value="1">bob</option>
    			<option value="2">sally</option>
    			<option value="3">john</option>
    			<option value="4">mike</option>
    			<option value="5">kim</option>
    		</select> 
    	</p>
    </form>
    <p>another form</p>
    <form name="form2" method="post" action="javascript:formtree()">
    	<p>
    		<textarea name="txtArea" id="txtArea" style="width:20em;" rows="">type some text here</textarea>
    	</p>
    	<p>
    		<input type="submit" name="cmdSubmit" id="cmdSubmit" value="Submit">
    	</p>
    </form>
    <hr />
    
    
    <p>&nbsp;  </p>
    </body>
    </html>
    As I said, it works in IE, but in Firefox it gives me an error saying that the variable "win" is not defined on line 23. This is after it displays the title of the test page.

    is the variable getting lost somewhere after it displays the title?

    Any help with what is going on would be appreciated.

    Thanks,
    Aaron B. Davis
    Last edited by aaronbdavis; 10-11-2005 at 12:18 PM.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    FF does the document.write immediately, IE parses the whole function. win is therefore overwritten in FF.
    Code:
    	var str="<form>Form Tree for " + win.document.title + "<br />";
    	//documen
    	str+="<ul>";
    .
    .
    	document.write(str+="</ul></form>" +
    				   	"End of Form Tree");
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles