【发布时间】:2012-06-02 02:24:48
【问题描述】:
我不知道为什么这不起作用:
var myurl = "http://domain.com";
var currenturl = $(location).attr('href');
if (myurl == currenturl) {
...
myurl 和 currenturl 都是一样的,但是 if 语句中的脚本没有被执行。
编辑:
$(document).ready(function(){
var myurl = "http://domain.com";
var currenturl = window.location.href;
//alert(currenturl);
if (myurl === currenturl) {
alert('should see this');
} else {
}
});
解决方案:
$(document).ready(function(){
var myurl = "http://domain.com/"; // see the slash at the end
if (myurl == location.href) {
alert('that ss the same page');
} else {
}
});
有什么建议吗? 谢谢。
【问题讨论】:
-
什么是
location?是window.location吗? -
或者您在寻找 $("#location")?
-
你的意思是
window.location.href? -
添加了完整的 sn-p,不起作用。
currenturl应该是访问者所在的页面。
标签: jquery url location href attr