最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript: Navigate to a new URL without replacing the current page in the history (not window.location) - Stack Overf

programmeradmin0浏览0评论

I know how to use window.location. The problem is that, (at least in FF) it erases the page you are on from the history. e.g., I'm on page A, I navigate to B and window.location is used to send me to C. If on page C, I click back, I end up at A, not B.

Is there some other way to navigate to another page without erasing the current page from the history?

EDIT: This wont happen if you run the code in Firebug.

Here is a super simple page (yes I know the code is ugly, it's not the real code) that shows the problem:

<html>
<head><script type="text/javascript" src=".4.2.min.js"> </script>
<body>  
<div class="foo"></div>
<a href="javascript:$('.foo').html('<scri'+'pt type=&quot;text/javascript&quot;>window.location = &quot;c.html&quot;</s'+'cript>');">b</a>

EDIT2: jQuery is the problem and setTimeout is the answer. I've added my solution below just in case someone else runs into this weird edge case.

EDIT3: This example is simplified to make it easier to test. In the real case, the click handler makes an Ajax call that returns the HTML, so we can't simplify the whole thing by just writing window.location = whatever. The code es from the server, embedded in the HTML.

I know how to use window.location. The problem is that, (at least in FF) it erases the page you are on from the history. e.g., I'm on page A, I navigate to B and window.location is used to send me to C. If on page C, I click back, I end up at A, not B.

Is there some other way to navigate to another page without erasing the current page from the history?

EDIT: This wont happen if you run the code in Firebug.

Here is a super simple page (yes I know the code is ugly, it's not the real code) that shows the problem:

<html>
<head><script type="text/javascript" src="http://code.jquery./jquery-1.4.2.min.js"> </script>
<body>  
<div class="foo"></div>
<a href="javascript:$('.foo').html('<scri'+'pt type=&quot;text/javascript&quot;>window.location = &quot;c.html&quot;</s'+'cript>');">b</a>

EDIT2: jQuery is the problem and setTimeout is the answer. I've added my solution below just in case someone else runs into this weird edge case.

EDIT3: This example is simplified to make it easier to test. In the real case, the click handler makes an Ajax call that returns the HTML, so we can't simplify the whole thing by just writing window.location = whatever. The code es from the server, embedded in the HTML.

Share Improve this question edited Mar 6, 2010 at 3:25 munity wiki
7 revs, 2 users 100%
noah 2
  • It would help if you posted the code you're using, e.g. window.location = url or window.location.replace(url) – Patrick McElhaney Commented Feb 24, 2010 at 18:43
  • I've tried both. Same result. – noah Commented Feb 24, 2010 at 20:20
Add a ment  | 

5 Answers 5

Reset to default 3

setTimeout is the answer.

$(div).html('<script...> setTimeout(function() { window.location = url; },10); </script>');

Doesn't have this problem. It must have something to do with the way jQuery executes inline scripts (by creating a script tag in the head element).

You could use window.location.assign(URL).

I'm not seeing that behavior in Firefox 3.6. Following your example, from stackoverflow. I went to google., then from there I used the Firebug console window to set window.location = 'http://facebook.'. Once there, all three were in my browser history..

This is just a hack and I don't know if it will work. Try window.history.back(1) after you have set the URL using window.location = URL;

Hope this helps.

Use location.href instead of location.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论