带有哈希标记的javascript重定向 [英] javascript redirect with hash tag

查看:79
本文介绍了带有哈希标记的javascript重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 History.js 通过AJAX实现了我网站的搜索结果。对于HTML5浏览器,我有类似 http://example.com/search/X6a2/3 的URL,在不支持History API的浏览器中,确实可以很好地回退到 http://example.com/search/#X6a2/3

I have implemented my website's search results with AJAX, using History.js. For HTML5 browsers, I have URLs like http://example.com/search/X6a2/3, which, in browsers not supporting the History API, does fall back nicely to http://example.com/search/#X6a2/3.

但是,如果HTML5 URL是在较旧的浏览器中打开时,页面一旦更改,新的哈希标签就会附加到完整的URL中。 (例如 http://example.com/search/X6a2/3#/X6a2/4 -ugh!)

However, if a HTML5 URL is opened in an older browser, the new hash tag is appended to the full URL as soon as the page is changed. (for example http://example.com/search/X6a2/3#/X6a2/4 - ugh!)

我需要的是一种一旦检测到非HTML5浏览器即可重定向到仅哈希标记版本的干净方法。 window.location.replace()似乎忽略了哈希标签。我该怎么办?

All I need is a clean way to redirect to the hash tag-only version as soon as a non-HTML5 browser is detected. window.location.replace() seems to ignore the hash tag. What do I do?

推荐答案

这似乎对我有用。您确定window.replace是问题吗?

This seems to work for me. Are you sure window.replace is the problem?

var href = "http://example.com/search/X6a2/3";
var idx = href.indexOf('search', 0);
var new_href = href.slice(0,idx+7) + "#" + href.slice(idx+7);

alert(new_href);
window.location.replace(new_href);

这篇关于带有哈希标记的javascript重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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