我必须使用jQuery来实现这一点......
当用户访问我网站上的任何页面(来自其他网站)时,必须发生这种情况(路径名不同)
http://www.mywebsite.com/pathname/
成为
http://www.mywebsite.com/#!/pathname
我需要语法方面的帮助。
非常感谢您的时间和帮助。
答案 0 :(得分:2)
$(document).ready(function(){
var pieces = location.href.split('/');
if (pieces[3].indexOf('#!') !== 0) {
if (location.href.indexOf('#!') != -1) {
pieces = location.href.replace('#!','').split('/');
}
pieces[3] = '#!/'+pieces[3];
location.href = pieces.join('/');
}
});
隐藏内容:
// I don't know you actually need this, since it may hide content
// in some cases when the redirect may not work, which might give
// the user a blank white page.
// Alternatively, you could use CSS as Ben notes, although you would
// only need display: none on the body tag.
$('body').ready(function(){$(this).hide();});
// Note, this is NOT in a handler.
var pieces = location.href.split('/');
if (pieces[3].indexOf('#!') !== 0) {
if (location.href.indexOf('#!') != -1) {
pieces = location.href.replace('#!','').split('/');
}
pieces[3] = '#!/'+pieces[3];
location.href = pieces.join('/');
}
答案 1 :(得分:0)
您不需要使用jQuery执行此操作。您可以使用window.location.href = "new url";
但如果你坚持......
var url = $(location).attr("href");
var path = url.substring(url.indexOf("com")+3);
var newurl = "http://www.mywebsite/#!" + path;
$(location).attr("href",newurl);
答案 2 :(得分:0)
你可以尝试这个(未经测试):
var pathname = window.location.pathname;
var hostname = window.location.hostname;
var newPath = pathname.replace(pathname, "#!" + pathname);
window.location.href = hostname + pathname;
答案 3 :(得分:0)
它的价值
var blah = document.referrer.substr(document.referrer.indexOf("/")+2);
if(blah.substr(0,blah.indexOf("/")) != location.host) location.href = (location.host + "/#!" + location.pathname);