/    Sign up×
Community /Pin to ProfileBookmark

Browser ‘back’ button problem

Hi everybody!
I’m scripting simplest AJAX request
On page I’ve got

[CODE]<% unless @user_favourites.include?(@entity.id) %>
<div id=”favourites_add_box” >
<a href=”?fav=added” onclick=”add_to_favourites(<%= @entity.id %>); return false;”>
<%= image_tag(“/images/go.gif”) %>
</a>
</div>
<% else %>
<div>Added</div>
<% end %>[/CODE]

and a javascript is

[CODE]<script type=”text/javascript”>
var http;
var browser = navigator.appName;
if(browser == “Microsoft Internet Explorer”) {

http = new ActiveXObject(“Microsoft.XMLHTTP”);

} else {
http = new XMLHttpRequest();
}
function add_to_favourites(eid) {
http.open(“get”, “/favourites/add/” + eid);
http.onreadystatechange = entity_added_response;
http.send(null);
return false;
}
function entity_added_response() {
if (http.readyState == 4) {
response = http.responseText;
a = document.getElementById(“favourites_add_box”);
if (a) a.innerHTML = “Added”;
}
}
</script>[/CODE]

The point is: when user click on the “add to favourites” link then content of <div> is replaced with text “Added” when ajax request successfully completed. This works fine. But when I go to another page on my site and then press ‘back’ browser button “Add to favourites” link is shown on page, like I’ve never pressed it before. If I reload page then proper text “Added” is shown.
So the problem is – after ajax call page is changed, but when I go to another page and press browser ‘back’ button page is shown unchanged.
Thanks everybody in advance.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jamesbcox1980Nov 16.2009 — This is a caching problem, I suspect. It's a common issue when dealing with Ajax.

Just before [CODE]http.send(null);[/CODE]
Try adding the lines:[CODE]http.setRequestHeader("pragma","no-cache");
http.setRequestHeader("Cache-Control", "no-cache, max-age=0");[/CODE]


See if that fixes the problem.
Copy linkTweet thisAlerts:
@slaughtersNov 16.2009 — I think James is correct. You most likely are just having a caching problem.

OR

The server side file you are executing in add_to_favourites ("/favourites/add/" + eid" ?) is failing. It does not appear that you are looking at the value returned in the "response" field. You are just putting "Added" in the DIV regardless of the results from the server side call.
Copy linkTweet thisAlerts:
@jeffrey_smithauthorNov 16.2009 — Hi, firstly, thanks for your replies.

I've tried to insert these 2 lines of code but it works as it should ONLY in Internet Explorer. It doesn't work in Firefox, Chrome and Opera 8(

Is there any way to make it work in all these browsers?

P. S.

I've tested - response is proper, so there's no any problems on server side.
Copy linkTweet thisAlerts:
@jamesbcox1980Nov 16.2009 — Hmm... strange, that usually works for me. That's the only thing I see that would fix it. Other than that, I'm stumped.
Copy linkTweet thisAlerts:
@jeffrey_smithauthorNov 17.2009 — It works, but only for Internet Explorer 8(

Is that working for other browsers for you?
Copy linkTweet thisAlerts:
@jamesbcox1980Nov 17.2009 — IE is usually the only on that has caching problems. So, yeah, that normally fixes for all browsers for me. But if it doesn't work for you, then there's another problem which I can't see.
×

Success!

Help @jeffrey_smith 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.16,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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