Jump to content

dynamic content problem with browsers back button


yaragallamurali

Recommended Posts

Hi we have developed a web app. In the 1st html page based on the user interaction we are inserting some html into the page using javascript. when user moves to the 2nd page everything is fine. Now if user presses the back button then 1st html is displayed without the content that has been added to it using javascript. this is the biggest challenege now. if the users presses the back button it is not a problem but the content that we added is very important and it is missing in the 1st html when user comes back to it using back button. How to solve this problem?

Note: i am using post method to submit the data. when user presses the back or forward button the request is not comming to the server so unable to handle from the server side code.

Link to comment
Share on other sites

I am not using jquery. I am using just javascript. My code is simple. I am coding for html-4 as html-5 is not yet supported fully by any of the browsers. The following is the snippet

<html>   <head>     <script>       function insert(){          var str="<div>Enter The Value :<input type='text' name='magic'></div>";           document.getElementById("sky").innerHTML=str;       }     </script>   </head>   <body>     <form action="xxx" method="post">       Name : <input type="text"  name="name" >       <div id="sky" onclick="insert()">Press here for magic</div>       <input type="submit" value="submit" >     </form>   <body></html>
Edited by yaragallamurali
Link to comment
Share on other sites

There may be other ways but one way may work by using cookies. So in the insert function, you'll also create a cookie there indicating that the user has clicked and loaded the magic div. Also you'll have to create another cookie that stores the info the user typed into the magic input field.

 

You can create another function that runs when the page loads that checks if these cookies are set or not. if it is set, that means the user filled in the info and left the page and has come back. If this is the case, then within the check function or however you do it, extract the data from the cookie(s) the user entered into the magic input field and set it to the sky div innerHTML.

 

When I mentioned cookies, I mean JavaScript cookie(s) since you said you're unable to handle anything from the server-side.

 

Hope this helps.

Link to comment
Share on other sites

There may be other ways but one way may work by using cookies. So in the insert function, you'll also create a cookie there indicating that the user has clicked and loaded the magic div. Also you'll have to create another cookie that stores the info the user typed into the magic input field.

 

You can create another function that runs when the page loads that checks if these cookies are set or not. if it is set, that means the user filled in the info and left the page and has come back. If this is the case, then within the check function or however you do it, extract the data from the cookie(s) the user entered into the magic input field and set it to the sky div innerHTML.

 

When I mentioned cookies, I mean JavaScript cookie(s) since you said you're unable to handle anything from the server-side.

 

Hope this helps.

one decent solution. I fond one more using ajax. Anyway thank you for answering my question and for the time you spent.

Link to comment
Share on other sites

As you say there is a technique for using Ajax to check with the server to see if the session exists and what the session variables are set to. It is a well known Ajax problem and there is an established approach you can google for. HTML5 allows the history to be modified to get around this problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...