所以基本上我只想在url看起来像这样运行代码:www.example.com/#page
否则不要像example: www.example.com/whatever/#page, www.example.com/whatever/whatever#page
那样运行jquery ..,
我的代码:
$(window).load(function(){
if(window.location.hash == '#page'){alert('success');}
});
目前它会提醒每个包含#page的网址。有任何建议,谢谢!
答案 0 :(得分:1)
试试吧
$(window).load(function(){
if(document.location.toString().indexOf('/#page')!=-1){alert('success');}
});
答案 1 :(得分:0)
if (window.location.pathname == "/sample/sample" && window.location.hash == "#page")
或其他适合你的排列
如果您希望根据路径的深度进行操作,让我们说,仅在路径名上至少两个深度的页面上发出警报,那么您需要拆分路径名
window.location.pathname.split("/").length //this is the depth of the url
所以要结合这两个想法并适合你的例子,
var depth = window.location.pathname.split("/").length;
// pathname starts with a /, which adds one to the length of the array, so subtract
depth = depth -1;
// now run our checks
if (depth == 2 && window.location.hash == "#page") alert("success!");
else alert ("failure!");
答案 2 :(得分:0)
怎么样
$(window).load(function(){
var test = location.protocol + '//' + location.host + '/#page';
if(test == location){alert('success');}
});