Reload page?

J

joe

I have a Javascript page which needs to be dynamically changed depending on user
input. The whole page is written on document.write() output.

I am still new to Javascript and run into problems with page reload. When user
presses a button on my page most functions should clear the page and repaint it
using my wholepage() function. After a couple of repaint the page goes into some
error. Am I supposed to clear the div's in the page or wot?

The pseudo code is here:

<html> <head>
....
<SCRIPT LANGUAGE="JavaScript">
....
[lots of code & functions including wholepage()]
....
function wholepage() {
document.write("<body bgcolor=\"#CCCCCC\">");
document.write("<div id=\"Layer1\" style=\"position:absolute;");
....[lots of code]
document.write("</div> </body>");
return true;}
....

function updateit() {
wholepage();
return true;}

</SCRIPT>
</HEAD>

<SCRIPT LANGUAGE="JavaScript">
wholepage()
</SCRIPT>

</html>
 
A

ASM

joe a écrit :
I have a Javascript page which needs to be dynamically changed depending on user
input. The whole page is written on document.write() output.

I am still new to Javascript and run into problems with page reload. When user
presses a button on my page most functions should clear the page and repaint it
using my wholepage() function. After a couple of repaint the page goes into some
error. Am I supposed to clear the div's in the page or wot?

The pseudo code is here:

<html> <head>
...
<SCRIPT LANGUAGE="JavaScript">
...
[lots of code & functions including wholepage()]
...
function wholepage() {

prefer something like :

var txt = '';
txt += '<body bgcolor="#CCCCCC">';
txt += '<div id="Layer1" style="position:absolute;';

and so on

txt += '<\/div><\/body><\/html>';

then

with(document){open();write(txt);close();}
}
 
J

joe

ASM said:
joe a écrit :
I have a Javascript page which needs to be dynamically changed depending on user
input. The whole page is written on document.write() output.

I am still new to Javascript and run into problems with page reload. When user
presses a button on my page most functions should clear the page and repaint it
using my wholepage() function. After a couple of repaint the page goes into some
error. Am I supposed to clear the div's in the page or wot?

The pseudo code is here:

<html> <head>
...
<SCRIPT LANGUAGE="JavaScript">
...
[lots of code & functions including wholepage()]
...
function wholepage() {

prefer something like :

var txt = '';
txt += '<body bgcolor="#CCCCCC">';
txt += '<div id="Layer1" style="position:absolute;';

and so on

txt += '<\/div><\/body><\/html>';

then

with(document){open();write(txt);close();}
}

That what I'm doing but with document.write().
What does the close() do?
 
R

RobG

joe said:
I have a Javascript page which needs to be dynamically changed depending on user
input. The whole page is written on document.write() output.

That does not seem like a good idea. You should just modify the bits
that need to change, not re-write the entire page.

I am still new to Javascript and run into problems with page reload. When user
presses a button on my page most functions should clear the page and repaint it
using my wholepage() function. After a couple of repaint the page goes into some
error. Am I supposed to clear the div's in the page or wot?

Calling document.write() after the page has finished loading will result
in a call to document.open(), which will clear the entire page content.
In some browsers, all script other than the one currently executing are
removed from memory immediately, in others they persist until the new
page is written but then are gone.

"clear the div's" is meaningless.

The pseudo code is here:

<html> <head>
...
<SCRIPT LANGUAGE="JavaScript">

The language attribute was deprecated many years ago, type is required.

...
[lots of code & functions including wholepage()]
...
function wholepage() {
document.write("<body bgcolor=\"#CCCCCC\">");

When you call wholepage(), the above statement will clear the entire
content of the current document. It appears that you don't write a new
title element, so you now have invalid HTML.

Your HTML uses deprecated attributes, you should look at updating to
HTML 4 - version 4.01 became a recommendation in 1999.

document.write("<div id=\"Layer1\" style=\"position:absolute;");
...[lots of code]

Presumably that 'lots of code' includes writing all the script elements
back into the document too.
document.write("</div> </body>");

You should always finish with:

document.close();

otherwise some browsers will wait forever for you to do so.

return true;}

What does it return true to?

[...]
 
J

joe

RobG said:
That does not seem like a good idea. You should just modify the bits
that need to change, not re-write the entire page.



Calling document.write() after the page has finished loading will result
in a call to document.open(), which will clear the entire page content.
In some browsers, all script other than the one currently executing are
removed from memory immediately, in others they persist until the new
page is written but then are gone.

"clear the div's" is meaningless.


I have a full sample here (below).
The page shows a "Hello" and "Press here". When user clicks the button at first
everything is OK. On second click IE shows "Error on page"


<SCRIPT LANGUAGE="JavaScript">
function mybutton(nap) {
window.alert("You pressed "+nap.toString());
wholepage();
return true; }

function wholepage() {
document.write("<html> <head>");
document.write("<title>Test page</title>");
document.write('<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">');
document.write('<body bgcolor="#CCCCCC">');
document.write('<div id="Layer1" style="position:absolute; width:600px;
height:230px; left: 10px; top: 50px; z-index:1">');
document.write('<table width="590" border="1" height="172">');
document.write("<tr>");
document.write('<td height="25" width="404" bgcolor="#999999" colspan="2">');
document.write("Hello");
document.write('<input type="BUTTON" name="submit" value="Press here"
onClick="mybutton(1);">');
document.write("</td></tr>");
document.write("</table> </div> ");
document.write("</body></HEAD></html>");
document.close();
return true;}
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
wholepage();
</SCRIPT>
 
E

Evertjan.

joe wrote on 09 mei 2006 in comp.lang.javascript:

I have a full sample here (below).
The page shows a "Hello" and "Press here". When user clicks the button
at first everything is OK. On second click IE shows "Error on page"


<SCRIPT LANGUAGE="JavaScript">

use: said:
function mybutton(nap) {
window.alert("You pressed "+nap.toString());
wholepage();
return true; }

function wholepage() {
document.write("<html> <head>");
document.write("<title>Test page</title>");

this cannot work, since after the first interactive document.write()
your javascript is overwritten and gone!!!!
 
A

ASM

joe a écrit :
I have a full sample here (below).
The page shows a "Hello" and "Press here". When user clicks the button at first
everything is OK. On second click IE shows "Error on page"

that can't work
and
that can't work

on second click function mybutton(nap) is unknown !

because
your document.write doesn't write the JS code for this function
 
J

joe

Evertjan. said:
joe wrote on 09 mei 2006 in comp.lang.javascript:





this cannot work, since after the first interactive document.write()
your javascript is overwritten and gone!!!!

How would my page be written then?
I thought the <script> - </script> part would stay in client memory.
 
E

Evertjan.

joe wrote on 09 mei 2006 in comp.lang.javascript:
How would my page be written then?
I thought the <script> - </script> part would stay in client memory.

where in memory?
 
A

ASM

joe a écrit :
How would my page be written then?

use an external javascript
it would be easier
I thought the <script> - </script> part would stay in client memory.

yes
except if you call a new page
and your javascript write a new page (or file)

this new page must have the same javascript written in it as the
original page
 
J

joe

ASM said:
joe a écrit :

use an external javascript
it would be easier


yes
except if you call a new page
and your javascript write a new page (or file)

this new page must have the same javascript written in it as the
original page


I am used to writing C and Javascript is giving me grey hair! I can't see what is
wrong from a C programmers point of view.

What I am looking for is a page that can be somehow changed whether by repainting
the whole page or some part of it. How do I issue a reload without my variables
being reset?
 
A

ASM

joe a écrit :
I am used to writing C and Javascript is giving me grey hair! I can't see what is
wrong from a C programmers point of view.

I don't know C
I only know that your JS is forgotten when you document.write a new page
Your document.write has to be a part of your page.

I don't know how to tell it
see and try 2 examples bellow.
What I am looking for is a page that can be somehow changed whether by repainting
the whole page or some part of it. How do I issue a reload without my variables
being reset?

Exercise :

<html>
<script type="text/javascript">
function getIt() {
var u = self.location.search;
if(u && u.length>0) {
u = u.split('=')[1]
var f = document.forms[0].elements;
f[0].value = u;
}
else alert('no data to get');
}
function sendIt() {
var f = document.forms[0].elements;
var u = f[0].value;
if (u=='') {
alert('field is empty');
f[0].focus();
f[0].select()
return false;
}
else
f.action=self.location;
return true;
}
onload = getIt;
</script>
<form action="" method="get" onsubmit="return sendIt();">
<p>Put something here :
<input name="myData" type="text">
<input type="submit" value="GO">
</form>
</html>



Application in html with 'document.write' :

<html>
<script type="text/javascript">
function getIt() {
var u = self.location.search;
if(u && u.length>0) {
u = u.split('=')[1]
var f = document.forms[0].elements;
document.write('My new Data is : '+u);
}
else alert('no data to get');
}
function sendIt() {
var f = document.forms[0].elements;
var u = f[0].value;
if (u=='') {
alert('field is empty');
f[0].focus();
f[0].select()
return false;
}
else
f.action=self.location;
return true;
}
</script>
<form action="" method="get" onsubmit="return sendIt();">
<p>Put something here :
<input name="myData" type="text">
<input type="submit" value="GO">
</form>
<p>
<script type="text/javascript">
getIt();
</script>
</p>
</html>
 
R

RobG

joe said:
I am used to writing C and Javascript is giving me grey hair! I can't see what is
wrong from a C programmers point of view.

What I am looking for is a page that can be somehow changed whether by repainting
the whole page or some part of it.

You need to learn about the basics of scripting web pages.

document.write is from the very beginning of JavaScript, there is no
public standard relating to how it should work. However, its behaviour
has been standardised to a large extent.

You can call document.write as the page loads to put content into the
page, however once the page has loaded (that is, the onload event has
occurred), a call to document.write will automatically also call
document.open.

Calling document.open opens a new document in the current window, which
is essentially the same as navigating to a new URL - everything in the
current page is deleted (ignoring caching, which is totally non-standard
and differs from browser to browser). The window object is also
deleted, it is a different entity to the browser window.

Scripts may hang around for a little while, but in most browsers will be
gone by the time the new document is displayed and certainly afterward -
objects and variables do not persist between documents (a pretty
fundamental principle).

Get your head around the document object model (DOM), because that is
what you are dealing with. From the point of view of JavaScript, the
browser is just a framework that supports the DOM.

How do I issue a reload without my variables
being reset?

You can't. You can store a small amount of data in cookies to use
between pages, but that is unreliable. You can also use XMLHttpRequest
and hidden iframes to load content from the server without loading a new
page, but that too can be unreliable (particularly support for
XHMHttpRequest when older browsers and mobile devices are taken into
consideration).

The usual idea for the web is to implement client/server data exchange
functionality using forms, then introduce script to speed things up and
replace form submission with XMLHttpRequest or to submit forms from
hidden iframes.

The best idea is to outline what you are trying to do from a
requirements perspective, show the code or test case you've developed to
implement it, then seek help here.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top