【发布时间】:2012-06-10 22:36:06
【问题描述】:
我正在实现一个网站,当新部分通过 ajax 加载到首页时,它会使用 History.js 动态设置其网址。
这似乎运作良好,但 History.js 作为 Internet Explorer 中的后备创建的 url 中的哈希部分存在问题。
以下是页面上的链接示例,使用 jquery 创建:
function connect_browse_buttons(){
$('.browselink').each(function(){
$(this).click(function(){
var action = $(this).attr('name');
action = action.substring( ('action_browse').length );
browsetype = action;
if (isIE){
// remove data object and title to avoid use of SUIDs by History.js in IE
History.pushState(null, null, '/public/' + action);
} else {
History.pushState({oldurl: History.getState()['url']}, "Example " + action, config.wwwroot + "public/" + action);
}
return false;
});
});
}
.htaccess 文件将任何 url(例如 http://example.com/public/category_a)重定向到 http://example.com,其中 javascript 解析 url 并通过 changeState 处理程序中的 ajax 请求加载适当的部分。
javascript 会检查诸如
之类的 urlhttp://example.com/public/category_a
AND 对于在 Internet Explorer 中创建的等效后备 URL,即
http://example.com/#public/category_a
一切正常 - 所以:
在 Firefox 中,如果我在站点的根目录 http://example.com 打开站点,然后单击上述链接,则内容会加载(在 changeState 处理程序中),然后url 由 History.pushState 设置为:
http://example.com/public/category_a
如果我然后点击另一个链接,则 url 设置为,例如:
http://example.com/public/category_b
在 IE 中,如果我在站点的根目录下打开站点,然后单击一个链接,如上所述,内容会加载,并且 URL 设置为带有哈希值:
http://example.com/#public/category_a
如果我然后单击下一个链接,则 url 设置为:
http://example.com/#public/category_b
问题出现了,当我在 IE 中打开一个在 Firefox 中添加书签的页面时,并且 URL 中没有哈希。让我们以我们通常的例子为例:
http://example.com/public/category_a
如果我直接在 IE 中打开此 url,通过书签或将 url 粘贴到浏览器地址栏中,.htaccess 重定向成功,js 文件解析 url 并加载内容。但是,现在如果我单击 category_b 链接,则该 url 由 History.pushState 设置为:
http://example.com/public/category_a#./category_b
我真正想要的是将网址设置为:
http://example.com/#public/category_b
然而,History.js 似乎将整个前一个 url 作为后续 pushStates 的基本 url。我尝试在 History.pushState 中设置绝对网址,但没有成功。正如您在上面的代码块中看到的,我有一个特定于 IE 的 pushState 语句。我尝试过以各种方式配置它。如何让 History pushState 识别:
http://example.com
作为 url 的基础部分,应该将哈希部分附加到哪个部分? 或者有没有比我上面描述的方法更好的方法来解决这个问题?
【问题讨论】:
-
嗨,您是否设法找到任何解决页面刷新时浏览器加载第一个网址而不是当前网址的问题??
-
你读过这个问题了吗?:stackoverflow.com/questions/14342912/…也许你能找到一些有用的提示
-
为什么不做 pushstate 到 public/category_a 并“重定向”到 # 以在 pushstate 工作时删除散列?
标签: jquery internet-explorer url pushstate history.js