JavaScript: Show / Hide <DIV> based on URL string

拥有回忆 提交于 2019-12-24 03:17:14

问题


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

回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/1666880/javascript-show-hide-div-based-on-url-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!