我可以使用javascript更改地址栏中的URL字符串吗? [英] Can I change the URL string in the address bar using javascript

查看:87
本文介绍了我可以使用javascript更改地址栏中的URL字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一个链接,说'关于'。单击它会加载一个特定的div,而不使用jquery .load()刷新整个页面。这不会更改浏览器地址栏中的URL字符串。

I've a link on my webpage, say 'about'. Clicking on it loads a particular div without refreshing the whole page using jquery .load(). This does not change the URL string in the browser address bar.

访问 www.mydomain.com/可以访问同一页面? page = about

所以我想做的是,当用户点击关于链接时,页面将被加载因为它(使用jquery),但我想更改浏览器地址栏中的URL字符串,以便有人可以实际复制或书签确切的页面。

So what I want to do is, when the user clicks on the 'about' link, the pages will be loaded as it is (using jquery), but I want to change the URL string in the browser address bar also so that someone can actually copy or bookmark the exact page.

是有可能吗?

推荐答案

你有两个可能来解决这个问题:

You have two possibilites to tackle this problem:


  • 在较新的浏览器中,您可以使用 HTML5历史API ,它允许更改URL的一部分,也包括查询字符串(和路径afaik)。

  • In newer browsers you can make use of the HTML5 history API, which lets change part of the URL, also the query string (and path afaik).

在不支持此功能的浏览器中,您只能在不重新加载的情况下更改片段标识符页面(通过 location 对象)。这意味着您必须将URL更改为例如

In browsers which don't support this, you can only change the fragment identifier # without reloading the page (via the location object). That means you have to change the URL to e.g.

www.mydomain.com/#!page=about

#!是Google的一项约定,旨在使Ajax网站可以抓取。由于片段标识符未发送到服务器,您必须使用JavaScript检测它并从服务器加载相应的数据。

jQuery插件,可帮助您处理此问题。

#! is a convention from Google to make Ajax sites crawlable. As the fragment identifier is not sent to the server, you have to detect it with JavaScript and load the corresponding data from the server.
There are jQuery plugins that help you to handler this.

我会寻找一个好的插件,如果有的话可以使用历史API,并回到基于散列的解决方案。

I would look for a good plugin makes use of the history API if available and falls back to the hash based solution.

如我的评论中所述,您还可以在此处找到更多信息:

As written in my comment, you can also find more information here:

  • How to change browser address bar without reloading page, especially @ThiefMaster's answer.

这篇关于我可以使用javascript更改地址栏中的URL字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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