Closed Bug 254678 Opened 20 years ago Closed 20 years ago

Javascript which refreshes window invalidates itself

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 114461

People

(Reporter: skatebiker, Unassigned)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040803

Refreshing a page with Javascript by using 
document.open("text/html", "replace")
invalidates the functions and global data of included JS files even when the 
same JS is included in the new content.

Reproducible: Always
Steps to Reproduce:
1.Create two files: test.html and test.js. Their content appears within --- 
lines in the 'Additional information' field.
2.Load the file test.html in Mozilla.
3.Click on the link 'reload'


Actual Results:  
Mozilla hangs and opening Javascript console (Tools-> Web Development -> 
Javascript console)
Error: hello is not defined.
Source file: file://[folder]/test.js


Expected Results:  
This should refresh the page and show the word <b>hello</b> in large letters. 
That happens on IE 6, Opera 7.2, Netfront 3.1 for Symbian phones.


The content of the test.html and test.js files:
--begin test.html---------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="">
<meta name="keywords" content="">
<title> jstest</title>
<script src=test.js></script>
</head>
<body>
Click on the link below. This should refresh the page and show the word 
<b>hello</b> in large letters. That happens on IE 6, Opera 7.2, Netfront 3.1 
for Symbian phones.
<p>
<a href='javascript:reload();'>reload</a>
<p>
However Mozilla 1.7 and firefox issue javascript errors 'variable hello not 
found'. 
It seems that reload by <tt>document.open("text/html", "replace")</tt> of the 
included .js file invalidates the validity all variables and functions in 
the .js file.
</body>
</html>
---end test.html--------------
And the following to a file called test.js
--begin test.js---------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="">
<meta name="keywords" content="">
<title> jstest</title>
<script src=test.js></script>
</head>
<body>
Click on the link below. This should refresh the page and show the word 
<b>hello</b> in large letters. That happens on IE 6, Opera 7.2, Netfront 3.1 
for Symbian phones.
<p>
<a href='javascript:reload();'>reload</a>
<p>
However Mozilla 1.7 and firefox issue javascript errors 'variable hello not 
found'. 
It seems that reload by <tt>document.open("text/html", "replace")</tt> of the 
included .js file invalidates the validity all variables and functions in 
the .js file.
</body>
</html>
---end test.js--------------
test.js should contain the following and NOT the same as test.html !!
Sorry, my fault !
--- begin test.js ----
var hello = "<h1>hello</h1>";

function reload()
{
    document.open("text/html", "replace")

       
    var s = "<html><head><title>jstest</title>";
    s += "<script src=test.js></script>\n";
    s += "<body>" + hello + "</body></html>";
    document.write(s);
    document.close();
}
--- end test.js ----
The script is executing in a global scope that gets wiped out by the
document.open.  So "hello" is undefined after that.... If you create the string
before calling document.open it will work.

*** This bug has been marked as a duplicate of 114461 ***
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.