Closed Bug 191437 Opened 22 years ago Closed 22 years ago

Inaccessible Global Variables after a document.open() when the page is loaded

Categories

(Core :: DOM: Core & HTML, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 76495

People

(Reporter: anthony.groyer, Assigned: jst)

Details

(Whiteboard: INVALID?)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3b) Gecko/20030130 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3b) Gecko/20030130008 You can't access a global variable after a "document.open()" when you called the function inside the "onload" event. (See Additional Information for the Source Code) If you called the function from inside the body of the HTML page (line 33), it works fine. Reproducible: Always Steps to Reproduce: 1. Open the source code in appendix 2. 3. Actual Results: The Alert Message Window appears and then a Javascript Error is reported. Expected Results: We should leave the global variables accessible. I noticed that it's not really clever to call "my_function" with a document.open() inside the onload event, because the page is already filled with contents and displayed when the event occurs. Maybe, you did the right thing, when I do document.open(), you clean up the previous contents of the page including the previous javascript declarations, in order to re-fill it. <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <SCRIPT LANGUAGE="JavaScript"> <!-- var my_global_variable = 1000; function my_function() { alert(my_global_variable); document.open(); //The browser can't access the variable "my_global_variable" here, after the document.open() //while it could accessed it at line 14. //We got an Javascript Error in the console. //It works fine with a local variable... document.write("the value of my global variable is " + my_global_variable); document.close(); } //--> </SCRIPT> </HEAD> <BODY onload="my_function()"> <SCRIPT LANGUAGE="JavaScript"> <!-- //my_function(); //It works fine if it's called from here. //--> </SCRIPT> </BODY> </HTML>
document.open() is ignored if the page is not done loading yet (when used in <body>). When used after the page has finished loading (in onload()) it creates a new document and clears the JS scope, since new documents get a new JS scope. This is the as-designed behavior, as I understand it.
Assignee: rogerl → jst
Component: JavaScript Engine → DOM Level 0
QA Contact: pschwartau → desale
Whiteboard: INVALID?
*** This bug has been marked as a duplicate of 76495 ***
Status: UNCONFIRMED → RESOLVED
Closed: 22 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.