/    Sign up×
Community /Pin to ProfileBookmark

back to previous page and refresh

Hi all,

I am looking for a cross-browser script that will send a user back to the previous page (history.go(-1)) AND refresh that page…

This code

<a href=”javascript:window.location.reload(history.go(-1));”>Back<a>

works perfectly in IE, but not in Mozilla/Firefox – any idea how I can adapt this code to work across all browsers? (including Safari)

Thanks,

Peter

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanOct 20.2004 — Why not just use history.go(-1)?

Tha reloads the page and sends them back to the last page they were on.

If not try using window.location.href anong with that ?


RyanJ
Copy linkTweet thisAlerts:
@JonaOct 20.2004 — [font=trebuchet ms]I've never heard of doing that, but anyway if you don't want the previous page to be in cache, use the appropriate META headers.[/font]

<i>
</i>&lt;meta http-equiv="Pragma" content="no-cache"&gt;
&lt;meta http-equiv="Expires" content="-1"&gt;
Copy linkTweet thisAlerts:
@palizauthorOct 20.2004 — Okay, perhaps I had better explain it better...

I have a shopping cart - the user adds a product to his basket and is then taken to the basket. He now has the option to continue to payment, empty the basket or to go back to the page he was on to continue shopping. This works perfectly with history.go(-1) - however, at the top of each page is his cart, which shows a total of the items in there - by using the normal history.go(-1) this cart does not update as the page is not refreshed to pull in the updated cookie - the user has to manually refresh the page to see his updated cart.

What I want is that when the user clicks on 'continue shopping' in his basket, he is taken back to the page he came from and the page is refreshed to show the updated cart at the top.

Now, the code javascript:window.location.reload(history.go(-1)); works a dream in IE, but in Mozilla it just refreshes the current page and messes up the CSS styles. I'm looking for a variation of this that works across all browsers.

...I have just thought of another problem...in the basket the user has the option to update his order, ie. increase the quantity of the items or delete an item...the 'update cart' button refreshes the cart. Now, if the user clicks on 'continue shopping' he is just taken back to the same cart page instead of the items he was looking at...

Any way around this? Perhaps using a session cookie that remembers the page he came from even though he updates the cart a few times before he wants to go back to the product pages?
Copy linkTweet thisAlerts:
@JonaOct 20.2004 — [font=trebuchet ms]I would use whatever server-side language you are already using to store a session cookie for the duration of the user's shopping (until he logs out). The session cookie would hold the referring document so that you can run some tests on it and then redirect to that page and refresh it without even having to use JavaScript. This way users without JavaScript (9% of those on the Internet) will still be able to shop.[/font]
Copy linkTweet thisAlerts:
@sciguyryanOct 20.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]I would use whatever server-side language you are already using to store a session cookie for the duration of the user's shopping (until he logs out). The session cookie would hold the referring document so that you can run some tests on it and then redirect to that page and refresh it without even having to use JavaScript. This way users without JavaScript (9% of those on the Internet) will still be able to shop.[/font] [/B][/QUOTE]



A very good point Jona ?


RyanJ
Copy linkTweet thisAlerts:
@palizauthorOct 20.2004 — Thanks Jona, that is a plan...I'm already using a cookie to store the shopping cart, so could perhaps add to that?
Copy linkTweet thisAlerts:
@palizauthorOct 20.2004 — or, I could do away with the user being taken to the basket when he adds a product - wouldn't it be simpler to just add the item to the cart, then when the user is ready to checkout then he could go to the cart - then there would be no need to have the referring page refresh if he wants to return to continue shopping...if that makes sense.

On this site the users do not log in - they enter their details each time (the owners idea, not mine!) - the cookie is created when they add the first item to the basket...
Copy linkTweet thisAlerts:
@JonaOct 20.2004 — [font=trebuchet ms]Ah, I see. Well yes, you could do that either way.[/font]
Copy linkTweet thisAlerts:
@palizauthorOct 20.2004 — Well, thanks guys - I'll give the session cookie a try...

Cheers!
Copy linkTweet thisAlerts:
@JonaOct 20.2004 — [font=trebuchet ms]Good luck![/font]
×

Success!

Help @paliz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...