开发者

Hide a div based on the URL

开发者 https://www.devze.com 2023-04-02 06:08 出处:网络
I am trying to hide a div whenever someone goes to a specific url. Something like this: var url = document.location.href;

I am trying to hide a div whenever someone goes to a specific url.

Something like this:

var url = document.location.href;

if (url.indexOf('http://donorperfect.local/asp/loginfull.asp') >= 0) {
    开发者_如何学C$('#hidebox').hide();
} else {
    $('#hidebox').show();
}

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


Run your code after the page is loaded and the element to hide is available to jQuery. Also convert the url into lower case and compare in case user types in mixed cases.

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

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


EDIT

Going off of the link that you provided as an example, there are several issues here.

  1. Your SCRIPT tag should be in the HEAD block
  2. You are using $() when it is not available (Firebug gives a clear error on this)
  3. The file name does not match your indexOf() match

Fixing these issues, it works fine. See:

<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

The following code works (setTimeout is for demonstration purposes):

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/

Although this is probably what I would recommend (for instance, what happens if it's 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/


You can try changing document.location.href to window.location.pathname;

So your code now says

var url = window.location.pathname; 

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


This will help you!!! Try this. it grabs the URL from the address bar.

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();. try .empty();

0

精彩评论

暂无评论...
验证码 换一张
取 消