我正在寻找有效的js 如果是在a.html那么
window.location="http://www.a.com/1"
否则,如果在页面b.html那么
window.location="http://www.a.com/2"
答案 0 :(得分:1)
您必须先通过window.location.href
检查当前位置才能获取网址。
然后使用substr(string, begin, lenght (optional) )
将您的网址截断到最后
//Address of your website
var url="http://example.com/"
function foo(){
var path=substr(window.location.href,url.length);
switch(path){
case "a.html": window.location="http://www.example.com/1"; break;
case "b.html": window.location="http://www.example.com/2"; break;
}
}
//don't forget to call your function
但是,如果你有像http://example.com/a.html#about-us
这样的内部喜欢
以下是如何处理它
//Address of your website
var url="http://example.com/"
function foo(){
var path=substr(window.location.href,url.length).substring(0, window.location.href.indexOf('#'));
switch(path){
case "a.html": window.location="http://www.example.com/1"; break;
case "b.html": window.location="http://www.example.com/2"; break;
}
}
//don't forget to call your function
了解更多信息
SUBSTR() EndUpateResource
关于Substring http://www.w3schools.com/jsref/jsref_substr.asp和