开发者

Hide and show controls on a condition

开发者 https://www.devze.com 2022-12-21 21:50 出处:网络
I have a url format http://www.xyz.com/DisplayPost.php?postid=200 On the page, I have a div called div1 which gets it data from Google Adsense (script).

I have a url format http://www.xyz.com/DisplayPost.php?postid=200

On the page, I have a div called div1 which gets it data from Google Adsense (script).

Here's w开发者_开发技巧hat I want to do:

Start by hiding the div. Then I want to detect if the url does not have "postid=250" in it and display the div.

How can I do it using jQuery. What css attribute to use to open the page by hiding the div and then displaying it based on the condition

Want to know the right way to do it.


You can use a regex to search for that string (no jquery necessary) and then you can use div1.css('display','none'); (assuming that div1 has already been variableized)


Use this code:

$(function(){
// hide the div initially
$("#div1").hide();

// Check to see if there is 250 in the url
var postID = getParameterByName('postid');

if (postID != 250)
{
  $("#div1").show();
}

});

// Javascript function to get query string value
function getParameterByName(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
0

精彩评论

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

关注公众号