jQuery的:更改URL地址,而不重定向? [英] jquery: change the URL address without redirecting?

查看:1665
本文介绍了jQuery的:更改URL地址,而不重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/1457/modify-address-bar-url-in-ajax-app-to-match-current-state">Modify在AJAX应用程序的地址栏中的URL以符合当前国家

我如何更改URL地址,而不重定向的页​​面?

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>

我会抢了URL的链接:

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;

我想要做这样的事情网站,当你点击在图像上, Ajax调用将获取请求的页面和窗口的URL地址也会变化,所以它有什么你单击一个路径。

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.

推荐答案

注: history.pushState()现在支持 - 看其他答案

NOTE: history.pushState() is now supported - see other answers.

您的不能更改整个URL重定向没有什么可以做的是改变

You cannot change the whole url without redirecting, what you can do instead is change the hash.

与#符号之后就是那张在URL的一部分。这是最初打算引导您(本地)到你的HTML文档的部分,但是你可以阅读和通过JavaScript修改它使用它的有点像一个全局变量的。

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:

  1. 在浏览器历史会记住你把每一个不同的步骤(因为URL +散改变)
  2. 您可以有哪些链接不仅可以在特定的html文件的地址,但也给你的JavaScript怎样做一个线索。这意味着你最终指向在你的web应用程序的状态。

要改变,你可以做的哈希:

document.location.hash = "show_picture";


要留意散列变化,你需要做的是这样的:

window.onhashchange = function(){
    var what_to_do = document.location.hash;    
    if (what_to_do=="#show_picture")
        show_picture();
}


当然,哈希只是一个字符串,所以你可以做pretty的,你想用它很多东西。例如,你可以把整个对象那里,如果你使用JSON来的字符串化的吧。


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.

有很好的JQuery库,做先进的东西这一点。

There are very good JQuery libraries to do advanced things with that.

这篇关于jQuery的:更改URL地址,而不重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆