jquery:更改 URL 地址而不重定向?
如何在不重定向页面的情况下更改 URL 地址?
例如,当我点击下面的这个链接时:
<a href="http://mysite.com/projects/article-1" class="get-article">link</a>
我将从链接中获取 URL:
var path = object.attr('href');
如果我在下面执行此操作,页面将被重定向:
window.location = path;
我想做这样的事情pentagram.com/work/#/all/all/newest/" rel="noreferrer">site,当您单击图像时,ajax 调用将获取请求的页面,并且窗口上的 URL 地址将也被改变,这样它就有了一条路径你点击。
Possible Duplicate:
Modify Address Bar URL in AJAX App to Match Current State
How can I change the URL address without redirecting the page?
For instance, when I click on this link below:
<a href="http://mysite.com/projects/article-1" class="get-article">link</a>
I will grab the URL from the link:
var path = object.attr('href');
If I do this below, the page will be redirected:
window.location = path;
I want to do something like this site, when you click on the image, the ajax call will fetch the requested page and the URL address on the window will be changed too, so that it has a path for what you click.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
注意:现在支持
history.pushState()
- 请参阅其他答案。您无法在不重定向的情况下更改整个网址,您可以做的就是更改哈希。
哈希是 URL 中 # 符号后面的部分。最初的目的是引导您(本地)访问 HTML 文档的各个部分,但您可以通过 javascript 读取和修改它,以使用它有点像全局变量。
如果应用得好,该技术有两个用途:
要更改哈希值,您可以执行以下操作:
要观察哈希值更改,您必须执行以下操作:
当然,哈希值只是一个字符串,因此您几乎可以执行以下操作你喜欢它。例如,如果您使用 JSON 对其进行字符串化,则可以将整个对象放在那里。
有非常好的 JQuery 库可以用它来做高级的事情。
NOTE:
history.pushState()
is now supported - see other answers.You cannot change the whole url without redirecting, what you can do instead is change the hash.
The hash is the part of the url that goes after the # symbol. That was initially intended to direct you (locally) to sections of your HTML document, but you can read and modify it through javascript to use it somewhat like a global variable.
If applied well, this technique is useful in two ways:
To change the hash you can do:
To watch for hash changes you have to do something like:
Of course the hash is just a string, so you can do pretty much what you like with it. For example you can put a whole object there if you use JSON to stringify it.
There are very good JQuery libraries to do advanced things with that.
请参阅此处 - http://my.opera.com/community/forums/topic.dml?id=1319992&t=1331393279&page=1#comment11751402
本质上:
您可以操纵历史对象使这项工作发挥作用。
它仅适用于同一域,但由于您对使用哈希标签方法感到满意,所以这应该不重要。
显然需要进行跨浏览器测试,但既然它发布在 Opera 论坛上,我可以放心地假设它可以在 Opera 中工作,而且我刚刚在 Chrome 中测试了它,它工作得很好。
See here - http://my.opera.com/community/forums/topic.dml?id=1319992&t=1331393279&page=1#comment11751402
Essentially:
You can manipulate the history object to make this work.
It only works on the same domain, but since you're satisfied with using the hash tag approach, that shouldn't matter.
Obviously would need to be cross-browser tested, but since that was posted on the Opera forum I'm safe to assume it would work in Opera, and I just tested it in Chrome and it worked fine.
该网站使用了 url 的“片段”部分:“#”后面的内容。浏览器不会将其作为 GET 请求的一部分发送到服务器,但可用于存储页面状态。所以是的,您可以更改片段而不会导致页面刷新或重新加载。当页面加载时,您的 JavaScript 会读取此片段并相应地更新页面内容,根据需要通过 ajax 请求从服务器获取数据。读取js中的片段:
但注意这个值会包含开头的“#”字符。设置片段:
That site makes use of the "fragment" part of a url: the stuff after the "#". This is not sent to the server by the browser as part of the GET request, but can be used to store page state. So yes you can change the fragment without causing a page refresh or reload. When the page loads, your javascript reads this fragment and updates the page content appropriately, fetching data from the server via ajax requests as required. To read the fragment in js:
but note that this value will include the "#" character at the beginning. To set the fragment:
你不能按照你的要求去做(而且链接的网站也没有完全做到这一点)。
不过,您可以修改
#
符号后面的网址部分,这部分称为片段,如下所示:片段可以 >不会发送到服务器(例如,Google 本身无法区分
http://www.google.com/
和http://www .google.com/#something
),但它们可以通过 Javascript 读取在您的页面上。反过来,这个 Javascript 可以根据片段的值决定执行不同的 AJAX 请求,这就是您链接到的网站可能执行的操作方式。You can't do what you ask (and the linked site does not do exactly that either).
You can, however, modify the part of the url after the
#
sign, which is called the fragment, like this:Fragments do not get sent to the server (so, for example, Google itself cannot tell the difference between
http://www.google.com/
andhttp://www.google.com/#something
), but they can be read by Javascript on your page. In turn, this Javascript can decide to perform a different AJAX request based on the value of the fragment, which is how the site you linked to probably does it.这是通过 URL 重写来实现的,而不是通过 URL 混淆来实现,后者是做不到的。
正如前面提到的,另一种方法是更改主题标签,
This is achieved through URL rewriting, not through URL obfuscating, which can't be done.
Another way to do this, as has been mentioned is by changing the hashtag, with
不,因为这会打开网络钓鱼的闸门。 URI 中唯一可以更改的部分是片段(
#
后面的所有内容)。您可以通过设置window.location.hash
来做到这一点。No, because that would open up the floodgates for phishing. The only part of the URI you can change is the fragment (everything after the
#
). You can do so by settingwindow.location.hash
.如果不进行重定向,您就无法真正更改地址栏中的整个 URL(考虑安全问题!)。
但是,您可以更改
hash
部分(#
之后的内容)并读取:location.hash
ps.通过以下方式防止链接的默认
onclick
重定向:You cannot really change the whole URL in the location bar without redirecting (think of the security issues!).
However you can change the
hash
part (whats after the#
) and read that:location.hash
ps. prevent the default
onclick
redirect of a link by something like: