html - Use Javascript history.go() to the real "previous" page - Stack Overflow

Assuming we have a history like this:- Home- Page 1- Page 2JS history.go(-1) works well to go to Page 1

Assuming we have a history like this:

 - Home
 - Page 1
 - Page 2

JS history.go(-1) works well to go to Page 1... but what if our current history is

 - Home
 - Page 1
 - Page 2
 - Page 2 (2nd time)
 - Page 2 (3rd time)
 - ...
 - Page 2 (nth time)

How can we use JavaScript history object to go back to Page 1?

Assuming we have a history like this:

 - Home
 - Page 1
 - Page 2

JS history.go(-1) works well to go to Page 1... but what if our current history is

 - Home
 - Page 1
 - Page 2
 - Page 2 (2nd time)
 - Page 2 (3rd time)
 - ...
 - Page 2 (nth time)

How can we use JavaScript history object to go back to Page 1?

Share Improve this question edited May 15, 2013 at 10:55 qwertoyo asked May 15, 2013 at 10:45 qwertoyoqwertoyo 1,4022 gold badges16 silver badges32 bronze badges 4
  • Use some jQuery history plugin like overset./2008/06/18/jquery-history-plugin – Kailash Yadav Commented May 15, 2013 at 10:49
  • What causes the same page to be saved in the history multiple times in a row? Hash in the url? – JJJ Commented May 15, 2013 at 10:49
  • It's a JavaScript limit that you can't read the real history. So basicly you can't determine if the previous page is the same as the current. You can only go back using an integer or as @Juhana said, using a trick with hashes. – Kevin Commented May 15, 2013 at 10:53
  • Users can "save and new"... this cause the POST of some data and the GET of the same page. Don't know why [some?] browser save again the page on the history. – qwertoyo Commented May 15, 2013 at 10:54
Add a ment  | 

3 Answers 3

Reset to default 1

You could create a function that would do a +1 to a property of an object, you could then use this value to go back the right amount of steps

var History = {

    steps : 0

}

somenewpagelink.onclick = function() {

    History.steps = (parseInt(History.steps)+1);

}

Then if you want to go back to page 1, simple create another function:

function goBack() {

    History.go(-History.steps);
    History.steps = 0;

}

When you go back the steps will be reset so when you browse further in your site, it won't go back to many steps.

I do think using jQuery is a good solution but not for small things that can easily be solved with raw Javascript, it will only slow your site down if using jQuery for 1 function.

Furthermore you could add a switch in there that if when you click back to actually go back from page 3 to page 1, you could make it reverse too.

function goBack(r) {

    if (r)
        history.go(History.steps);
    else 
        history.go(-History.steps);
        History.steps = 0;

}

Ofcourse this function would require the tweaking needed to reach the desired effect and be pletely flawless, but for the simple implementation you could go with this.

The signature for this method is history.go(number|URL) from (w3school). So you can use a string parameter instead of number, and if (in some manner) you know which was the url for page2 you can use it.

history.go("http://localhost/page2");

when page is loaded,record now history.length in a js object. when you want back to page1 ,history.go(record-history.length-1)。 I test it work well.but i dont know whether there will be something unsafe or not.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745244859a4618362.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信