2011-09-02 55 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();