473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

After clicking on back button, URL is changing but content is loaded in History api

1
My doubt is regarding history api feature of html5 which is using ajax script.

In my code after loading index.html, i have tried to open html content using link in central section of page. Upto this is done but when i click to back button then url is changing but content was of previous page.

following is my js code :
Expand|Select|Wrap|Line Numbers
  1. function supports_history_api() 
  2. {
  3. return !!(window.history && history.pushState);
  4. }
  5.  
  6. function page_cal(href)
  7.     var page_request = false;
  8.     if (window.XMLHttpRequest) // if Mozilla, Safari etc
  9.     page_request = new XMLHttpRequest();
  10.     else if (window.ActiveXObject) // if IE
  11.         {
  12.             try 
  13.             {
  14.             page_request = new ActiveXObject("Msxml2.XMLHTTP");
  15.  
  16.             } 
  17.         catch (e)
  18.             {
  19.                 try{
  20.                 page_request = new ActiveXObject("Microsoft.XMLHTTP");
  21.                 }
  22.             catch (e){}
  23.             }
  24.         }
  25.  
  26.     page_request.open("GET","../PA-20/pages/"+href.split("/").pop(),false);
  27.     page_request.send(null);
  28.     if (page_request.readyState==4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) 
  29.     {
  30.     document.getElementById("opencontent").innerHTML = page_request.responseText;
  31.     return true;
  32.     }     
  33.     return false;
  34. }
  35.  
  36.  
  37. function addClicker(link) 
  38. {
  39.  //alert(link+"1st");
  40.  link.addEventListener("click", function(e) 
  41.   {//alert(link+"2nd");
  42.     if (page_cal(link.href)) 
  43.     {  
  44.       alert(link.href+" Clicked !");
  45.     //var url = link.href;
  46.  
  47.     var linksElement = document.getElementById('id');
  48.     var pathname =  link.href;
  49.     var stateObj = { first: "home",
  50.                      second: "products",
  51.                      third:    "promoters",
  52.                      fourth: "aboutus",
  53.                      fifth: "contact",
  54.                     };
  55.  
  56.      history.pushState(pathname, null, link.href);
  57.      alert(history.state);
  58.          e.preventDefault();
  59.         }  
  60.    }, true);
  61.   }
  62.  
  63.  
  64.  
  65. function setupHistoryClicks()
  66. addClicker(document.getElementById("home"));
  67. addClicker(document.getElementById("products"));
  68. addClicker(document.getElementById("promoters"));
  69. addClicker(document.getElementById("aboutus"));
  70. addClicker(document.getElementById("contact"));
  71. addClicker(document.getElementById("kalonji"));
  72. addClicker(document.getElementById("chilli"));
  73. addClicker(document.getElementById("jaiphal"));
  74. addClicker(document.getElementById("jeera"));
  75. addClicker(document.getElementById("elaichi"));
  76. addClicker(document.getElementById("malkingini"));
  77.  
  78. }
  79.  
  80.  
  81.  
  82. window.onload = function(e) 
  83. {
  84.   if (!supports_history_api()) 
  85.   {return;}
  86.    setupHistoryClicks(); 
  87.    e.preventDefault();
  88.   window.setTimeout(function() 
  89.   {
  90.     window.addEventListener("popstate", function(e) 
  91.     { 
  92.     alert( " Back button is cliecked ! " + "location: " + document.location + ", state: " + e.state);
  93.  
  94.         location.reload();    
  95.  
  96.     page_cal(document.location);
  97.  
  98.     }, false);
  99.  
  100.       }, 1); 
Thanks in advance !

Regards,
Swappy
Feb 20 '12 #1
1 1999
acoder
16,027 Expert Mod 8TB
I think the following may help:

Manipulating the browser history
History.js (linked from above article)
Feb 27 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ed | last post by:
Hi, I have an html page with a div element within a form for dynamically creating textfields. The problem is when I click a link on the page, or the submit button, then click the back button,...
0
by: Earl Teigrob | last post by:
This is an odd one. When I first display a thumbnail page and the user clicks one of the image links, the user goes to the correct page. If the user uses my application back button, (called...
6
by: pmud | last post by:
Hi, I have created a very simple ASP.NET application which has a couple of ImageButtons which go to different SQL reports on clicking them. I have used Response.Redirect to send the user to the ...
1
by: gferreri | last post by:
Hi all, I've stumbled on an interesting problem with the way Firefox handles form submitting with the enter key. I'm putting together a page that has one form element with multiple controls...
10
by: mjahabarsadiq | last post by:
I would like to reload the page when it is redirected by the browser's Back or Forward button. I tried the following but it does not works. <link rel="prev" href="url"> <link rel="next"...
0
by: manojsingh | last post by:
hi, I am writing a code for logout.jsp and used invalidate() method of session object. The program runs perfectly but afte logout when i click back button of window it shows the previous page...
2
viswammamilla
by: viswammamilla | last post by:
Hai i did one application ,its an intranet web application using asp.net and vb.net. Once user login into the application and did some task whenever user hits back button it will come back to one...
1
by: jalpari | last post by:
hi all, i have created a form in which i have a button for searching records from database, when a record is found it'z displayed in the table fields and whenever the record'z not found it displays...
1
by: Jimmy White | last post by:
Note: I'm not trying to prevent back button! So I'm looking for solution, not a patch. Shortly: How to prevent going backwards to login and then refreshing the page (thus resending the username...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.