History Replacement And New Location Combined

A

abc

I have a set of web pages each containing a form with GET method. On
submit, I need to parse the form, add element values to the
location.search query, replace the current history location's URL with
the newly constructed one (updated form fields!), and THEN to invoke
the next page. Since each page would run restorePage() function on the
page load, each form would be restored on the browser's back button
request. Here's the brief overview of the logic:

<html>
<head>
<script>

savePage(){

// Parse the form and add parameters to the current location.search

a = location.search.substring(1);
for (i=0; i<form.length; i++){
a += '&' + form.elements.name + '=' + form.elements.value;
}

// Remember form fields' values
location.replace('currentpage.shtml?' + a);
// Pass them to the next page
setTimeout("document.location = 'nextpage.shtml?' + a;", 1000);
return false;
}

restorePage(){

// Parse the query and fill out the form

args = new Array();
pair = new Array();
a = location.search.substring(1);

args = a.split('&');

for (i=0; i<args.length; i++){

pair = args.split('=');
form.elements.value = pair[1];
}

</script>
</head>

<body onLoad="restorePage();">
<form method="get" action="javascipt: savePage();"
onSubmit="checkForm();">
:
:
</form>
</body>
</html>

As could be seen from the code above, I synchronize replace and load
requests, giving the replace request a full second to execute. But -
it doesn't work as expected! It does replace the URL correctly, but it
never loads the next page. On the other hand, if I comment out the
replace request, the next page loads correctly, but I lose the restore
functionality. It appears that these two requests are mutually
exclusive.

I've tested the thing in NS 7.1 on RedHat Linux. Didn't even try it in
IE or FF, assuming it wouldn't work either.

Any suggestion would be greatly appreciated.
 
A

abc

Not sure what you mean by "synchronize", but I think the location.replace
will stop your script dead in its tracks.

In his book "JavaScript - The Definitive Guide", David Flanagan gives
on page 248 an example of History and Location objects usage. He has a
form with 'back' and 'forward' buttons serviced by accompanying JS
functions. Here's just one of them:

<script>

// The function is invoked by the Back button in our navigation bar

function go_back(){

// First, clear the URL entry field in our form.
document.navbar.url.value = "";

// Then use the History object of the main frame to go back.
parent.frames[0].history.back();

// Wait a second, and then update the URL entry field in the form
// from the location.href property of the main frame. The wait
seems
// to be necessary to allow the location.href property to GET IN
SYNC.
setTimeout("document.navbar.url.value =
parent.frames[0].location.href;", 1000);
}

It seems that you are right about location.replace() stopping my
script: I've tried different gymnastics without any success.
I have to admit, I can't figure out what you are trying to accomplish here.

Just by asking your question "What are you trying to accomplish here?"
you helped me clear out my confusion! Now I realize that I do not need
coupled bookmarking and next page loading, and why they are aparently
mutually exclusive in JavaScript.
But I can tell you that you don't want a form with a javascript action.

I owe you a short application description here. It's about meat
cutting decision making tool for retail industry. Since I don't know
what decisions user might make (how many meat cuts out of the initial
primal, at what price, with what trimmings, etc.), I need a full
flexibility and independence on the client side (browser), which
assumes unloading the burden off the MySQL server and saving the
database into the web pages as a set of JS Array objects (delivered as
Server Side Includes). In such a context, I need JS for servicing
user's requests.

The meat cutting process happens through a set of pages containing
spreadsheets generated by JS and DOM. Since my client is a spoiled
one, he needs on top of it the possibility of leaving the process at
any point and continuing it later on. But, then again, since
bookmarking remembers only the URL used to invoke a page, I have to
provide functionality which would on the bookmark request remember the
current values of the spreadsheet in the current page, and substitute
the newly constructed URL for the initial one in the location bar.

I hope i didn't misunderstand your remark.
Can you post a link to a page that works without JS and how
you are trying to enhance the experience with JS?

Unfortunately - no, there are no pages w/o JS! On the contrary,
they're all JS intensive. You cannot reach the pages since they're
hosted by our password protected development server but, if you are
still interrested, I would be more than happy to send you the URL once
the application's posted on our production server.

Thank you very much indeed, David, for your pertinent questions and
please accept my apologies for my asking for help prematurely.

Cheers!

P.S. My first reply to you went into void this morning, so I had to
compose it one more time. I'm affraid I was not as constructive in my
second attempt. Sigh...
 
A

abc

Okay, he wants to let the browser catch up before setting the
location.href.

Sure, that's how I understood it myself and tried to use the same
solution.
You can do all of that, but you should look into the onsubmit event
of the form object. The way you are doing it is bad form
(no pun intended.)

In each and every page, I have up to four 'submit' buttons with their
respective onClick event handlers. If I used onSubmit event handler,
it would have to decide what event needs to be serviced, actually
getting me back (through one additional step) to the same logic I've
already applied.
Did you consider a cookie-based solution to persist the data?

Ever since their dawn I hated cookies, for multiple reasons! My
attitude didn't change much in the meantime...
No problem. Not sure what that means, but it is better than asking for help
when it is too late!

What I meant is that I should have pondered the problem a little more,
before asking for other people's help.

I expect the application to be done in about two months - I'll let you
know!

Thanks again for your help!
 
D

David Mark

David Mark said the following on 7/11/2007 7:55 PM:







No, you put the non-JS warning on the page and then you replace it using
JS. If you put it in a noscript element and the script has an error,
then the JS crowd gets nothing.

I don't agree with that. In an HTML document, I say just
document.write the form and pair it with a NOSCRIPT tag. In an XHTML
document that is actually served as XHTML, I would insert the nodes
that make up the form after the page loads. Under what circumstances
would such a script error?

If you do it the other way, you must either wait for the page to load
to replace the warning message or simulate the DOMContentLoaded for
browsers that don't support it (ie put the script near the bottom of
the page, possibly with a timeout and hope for the best.) If you go
with the former method, you get a flash of the warning message, the
latter is a lot of trouble and not foolproof (as few things are with
IE.)
 
D

David Mark

David Mark said the following on 7/12/2007 6:28 AM:





You are entitled to that opinion just as I am entitled to mine :)


It is far more reliable to make the noscript block part of the page and
then replace it with a form element using script.


Hmm. Ever seen a site that uses third party scripts? Think about Google
AdSense and many others that you have no control over. If you have one
in your page and there is an error in that script, then yours is now
broken and your visitor won't see anything at all.

That makes sense. I hadn't considered that as I didn't think the OP
was writing an app that would have third-party scripts. In that case,
the visitor would see an error message (at least in IE.)
I have never seen a flash of the warning message but I have never put
anything other than a short message and no images on the page.

That works if there are no unscripted attractions on the page. I was
just thinking about the form. I figured that even though the form
wouldn't do anything without script, the page would load a
presentation of some sort. If not and if third-party scripts are
involved, then I agree with you.

Then it
loads almost instantly then I place images/forms and other things. But,
on a slow computer with a graphics heavy page it could be a problem.

Right. I would be most wary of Opera as I see content flash without
style all the time with that thing. IE does fine if you schedule your
DOMContentLoaded routine just before the closing body tag. Watch out
if you are inserting nodes into the body though. I have never
encountered the infamous "operation aborted" error and perhaps IE6's
hotfixes have addressed the problem at this point, but that has got to
be the worst catastrophe possible for a Web page.
 
D

David Mark

David Mark said the following on 7/12/2007 5:51 PM:



They may, or may not, be writing one with a third party script. It is
just habit for me to do it the same way all the time and then it doesn't
matter. If you do it one way with third party and another way without,
you can tend to slip up and forget it sometimes.



I don't recall ever seeing, or even reading about, an "operation
aborted" error in IE6 but I haven't used it a long time. I

Google it and you will see what I mean. MS has information on it
their KB. They've probably fixed it by now, but they can't force
people to update their browsers. In short, you get a popup that
informs you that the page cannot be opened and nothing else. Using
innerHTML on or inserting nodes in the body before it has finished
loading is what causes it. There are lots of clumsy scripts out there
dedicated to working around it and none of them are foolproof. It is
best to just set a timeout immediately before body's closing tag.

*have* seen a
situation where an external script caused a webpage to completely broken
though. For some reason, my bank decided it "Had to have" a Google
search box and Google Ad's all over the page. Google's script and the
Bank's script clashed and the site became un-usable. I emailed

That is why I encapsulate everything I write these days in a single
object. You'd think Google would do the same.

the
web-master and explained what was wrong, he wasn't interested.

I know the type. Too stupid or lazy (or both) to fix anything. The
Web is teeming with developers like that.

How did
it get fixed? One of the guys in IT at the bank is a friend of mine and
called me and asked if I would look at it. Told him I didn't have to,
forwarded him the e-mail. He corrected the problem. When asked by his
boss how he fixed it, he showed him the e-mail. Suffice it to say, the
bank has a new web-master now. But, it is only one example of a

Good for you! Seriously. That's one less bum out there mucking up
the Internet.

real
world situation where it can cause a problem so it is best to try to
program in a defensive mechanism to try to prevent it. You can't always
prevent it but you can still try to be as defensive as possible with coding.

I get what you are saying. I agree that under some circumstances your
strategy makes sense. Most of my development experience is with
Intranets where you don't have to worry about third-party scripts, so
my coding style has evolved differently. I'm starting to do more
Internet work of late, so I will keep what you said in mind.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Hmm. Ever seen a site that uses third party scripts? Think about Google
AdSense and many others that you have no control over. If you have one
in your page and there is an error in that script, then yours is now
broken and your visitor won't see anything at all.

It may be that Google AdSense presents only fatal flaws.

But I very commonly find, using IE6/IE7, pages such as
<http://www.hdnews.net/Story/David_Dinges_071207>
which give me an error message but, when that is dismissed, present what
seems otherwise satisfactory - apart from the fixed text-size (in
1280*1024 17" crt) being suited to the lesser ants.

FF2 shows, on the error console, that and other errors for that page;
Opera does not mind.

Flaws in code (much of which is often in support of advertising and
unnecessary for the main content) do not necessarily prevent the visitor
from seeing what is wanted.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 7/13/2007 12:38 PM:

Google AdSense was only the example I used. The principle is the same.
You may want to read the entire thread and determine what context my
statement was made. If your page uses the onload event to alter the
page and there is a script error in the page then your content won't
get changed.

But "content won't get changed" is not the same as "won't see anything
at all".

Please remember that there are many here who have learned to read
English properly, at a (slightly) greater age than when you were taught
to read. Write carefully, so that your words express your exact
meaning, without reliance on earlier postings. AISB, a FAQ writer needs
to be literate in the languages of the FAQ.


He also needs to be accurate. In the following paqe, executed in IE6,
the word "Yes" is shown independently of whether the '==' is changed to
'=', even though, as posted, the code gives an error message too.

<body onload="Fn()">

<script>
function Fn() {
zz.innerHTML="Yes" }
</script>

<script>Randy == "Webb"</script>

<div ID=zz>No</div>
</body>
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top