2011-09-02 82 views
0

我想隐藏一个div,只要有人转到特定的网址。隐藏一个基于URL的div

事情是这样的:

var url = document.location.href; 

if (url.indexOf('http://donorperfect.local/asp/loginfull.asp') >= 0) { 
    $('#hidebox').hide(); 
} else { 
    $('#hidebox').show(); 
} 

write("<div id=\"hidebox\">\n"); 
write("<p>test</p>\n"); 
write("</div>\n"); 
+3

你会得到任何错误? – Neal

+0

确保在隐藏盒已被渲染后运行此程序 – karlipoppins

+0

您能向我们展示您的HTML代码吗? –

回答

2

运行你的代码后,页面加载和隐藏要素是提供给jQuery的。同时将url转换为小写,并在混合情况下比较用户类型。

$(function(){ 
    var url = document.location.href; 

    if (url.toLowerCase().indexOf('http://donorperfect.local/asp/loginfull.asp') >= 0) { 
     $('#hidebox').hide(); 
    } else { 
     $('#hidebox').show(); 
    } 
}); 
+0

我试过这个,它没有工作:( –

+0

你确定url匹配吗? – ShankarSangoli

+0

好吧我在测试页面设置这个,所以你们可以看到即时通讯谈论什么http://www.donorperfect.com/asp/ loginfull_matt.asp ...在标题附近,你会看到“test-this is the hidebox div”这个词.. –

0

你可以尝试改变document.location.hrefwindow.location.pathname;

所以,你的代码现在说

var url = window.location.pathname; 

    if (url.indexOf('http://donorperfect.local/asp/loginfull.asp') >= 0) { 
    $('#hidebox').hide(); 
    } else { 
    $('#hidebox').show(); 
    } 
+0

我试过了,它是一个不行:( –

+0

@Matt :不是一直说“它不工作”或“这是一个不行”,你能告诉我们什么_did_发生吗?做一些调试! –

+0

Tomalak - 当我说不去。或它不工作我意思是什么都没有发生,div没有消失,没有错误 –

2

编辑

都会响起,你作为一个例子提供的链接,有这里有几个问题。

  1. SCRIPT标签应在HEAD
  2. 您正在使用$()当它是不可用(萤火虫给出了一个清晰的错误)
  3. 文件名不符合您的indexOf()比赛

解决这些问题,它工作正常。请参阅:

<head> 
... 
<script language='JavaScript' src='/js/jquery-1.4.1.js' type="text/javascript"></script> 
... 
<script type="text/javascript"> 
$(function(){ 
    var url = window.location.href; 

    if (url.indexOf('donorperfect.html') > -1) { 
     $('#hidebox').show(); 
    } else { 
     $('#hidebox').hide(); 
    } 
}); 
</script> 
... 
</head> 

http://jfcoder.com/test/donorperfect.html

下面的代码工作(setTimeout用于演示目的):

document.write("<div id=\"hidebox\">\n"); 
document.write("<p>test</p>\n"); 
document.write("</div>\n"); 

$(document).ready(function(){ 
    var url = 'http://donorperfect.local/asp/loginfull.asp'; 

    if (url.indexOf('http://donorperfect.local/asp/loginfull.asp') > -1) { 
     setTimeout(function(){$('#hidebox').hide()},2000); 
    } else { 
     $('#hidebox').show(); 
    } 
}); 

http://jsfiddle.net/userdude/Qt8uH/

虽然这可能是什么,我会建议(例如,如果它是HTTPS,会发生什么情况?):

document.write("<div id=\"hidebox\">\n"); 
document.write("<p>test</p>\n"); 
document.write("</div>\n"); 

$(document).ready(function(){ 
    var url = 'http://donorperfect.local/asp/loginfull.asp'; 

    if (url.toLowerCase().indexOf('loginfull.asp') > -1) { 
     setTimeout(function(){$('#hidebox').hide()},2000); 
    } else { 
     $('#hidebox').show(); 
    } 
}); 

http://jsfiddle.net/userdude/Qt8uH/1/

+0

我复制了你在页面上的代码 –

+0

write(“ \ n”); (“(function(){\ n”); \t write(“var url ='http://www.donorperfect.com/asp/loginfull_matt.asp';//window.location.pathname; \ n“); \t write(”if(url.indexOf('loginfull.asp')> = 0){\ n“); \t write(”$('#hidebox')。hide(); \ (“} else {\ n”); \t write(“} else {\ n”); \t write(“$('#hidebox').show(); \ n”); \t write ); \t write(“} \ n”); \t write(“}); \ n”); \t write(“ \ n”); –

+0

从这个页面http://jfcoder.com/test/donorperfect.html,它仍然没有工作 –

0

这将帮助你!尝试这个。它从地址栏抓取URL。

var url = window.location.href; 
if(url == "http://www.promilitarybusinessnetwork.com/continueSearch.asp?categoryID=108") { 
    $('#website').html('<p>This is the Apartments Category page</p>'); 
} else { 
    $('#website').hide(); 
}}); 

if not .hide();.尝试.empty();