4

I need to show a DIV on two pages (URL's) but not on the others. (I have jQuery on the pages if that helps.). I'm a complete noob so all help is very much appreciate. Thank's!

Case (1) where I want to show the DIV:

  • On the start page, when the web browser address field reads 'www.mydomin.com'
  • The start page is PHP so I guess the full URL is 'www.mydomin.com/index.php'

Case (2):

  • 'www.mydomin.com/index.php?option=com_myblog&title.html&Itemid=1&lang=en'
  • WHERE this part is alway the same
  • 'www.mydomin.com/index.php?option=com_myblog&'
  • AND this part is always unique
  • 'title.html&Itemid=1&lang=en

Example

    if (url == 'www.mydomin.com' or 'www.mydomin.com/index.php?option=com_myblog&') {

 do this

    xxxxxxxx

 else

        nothing

4 Answers 4

7

This should work, if I understand the question correctly

var url = document.location.href;

if (url.indexOf('www.mydomin.com/index.php?option=com_myblog&') >= 0) {
  $('#div_id').hide();
} else {
  $('#div_id').show();
}

But really, if you use PHP anyway, you should figure out how to not render the div in the first place.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the code! I tried it but could not get it to work (it renders the div on all pages). I think you understood the question right except one thing - I also have to check it the URL is the start page 'www.mydomain.com/index.php' with no extra characters after the URL. Regarding the PHP suggestion I think you are absolutely right - my problem is that I have no control over the PHP because its generated from a database. So this is sort of a hack to get things on the page that I cant otherwise.
are you sure you replaced my #div_id with the correct selector?
1

You can parse the query string and show/hide the div based on the result.

1 Comment

Thank you for trying to help! I'm afraid I'm to much of a noob to understand all the information on the page you linked to. Sorry!
0

I also think it should be handled from PHP code instead from JavaScript. And div should not be rendered in first place.

1 Comment

Thank's. I agree I would if it was possible (pls see first comment).
0

You can target a specific query parameter of the URL using window.location.search. Using the below code, you can find an exact match anddisplay/hide the HTML element:

var firstURLParam = window.location.search.substring(1).split('&')[0];
var datGuiEle = document.getElementById("elemID");
if(firstURLParam == "debug"){
    datGuiEle.style.display = "block";
}
else {
  datGuiEle.style.display = "none";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.