Home » Wordpress » Hide div on custom page(url) load/open?

Hide div on custom page(url) load/open?

Posted by: admin November 30, 2017 Leave a comment

Questions:

On my web page NicoComerc when u open it, there is custom div at bottom left position to show “traditional pasta” – girl on every page that u open.
But i want to hide that div when u open this URL from the left menu. (that url is > Производи > Потпалувачи) <- sry its macedonian language…

So is it possible to do it, and if so… how to make that?

Thanks!

Answers:
<script>


$(function(){

var url = window.location.pathname;

if (url.toLowerCase().indexOf("portfolio=perun") >= 0){
$("#devojka").remove();
}

});
</script>

Required JQuery, but you already have it

Questions:
Answers:

Yes it’s possible. You can do that by checking if the url fits some pattern. In javascript, one way to achieve that is the following one …

Code

<script type="text/javascript">   
    if(window.location.search == "?page_id=2762" || window.location.search == "?portfolio=perun" ) { 
        $('#devojka').css('display','none'); 
    } 
</script>

You are calling the script too soon in your page. When $('#devojka') is executed, the element (devojka) is not in page yet.

Move you script to the bottom of the page or wrap your code to the window.onload.

<script type="text/javascript">   
    window.onload = function() {
        if(window.location.search == "?page_id=2762" || window.location.search == "?portfolio=perun" ) { 
            $('#devojka').css('display','none'); 
        }
    } 
</script>

More about window.location: https://developer.mozilla.org/en-US/docs/Web/API/Window.location

Questions:
Answers:

I would add a query string to the link in the menu (/menu/item?hide_div=1). Then you can check for the existence of string on the server side or with javascript.

Questions:
Answers:

You can just write JavaScript/JQuery such as the following

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?) (&|#|;|$)').exec(
             location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
$(function() {
    if (getURLParameter('portfolio') == 'потпалувачи-перун') {
        $('#id-of-div-to-hide').hide();
    }
});