开发者

Find in URL string from Array of values?

I am trying to find array values in URL string? check the condition If requested URL contains array values then pass condition otherwise redirect to google.com

example

     $(document).ready(function () {
        var urls = ["/dept/ce/",
                      "/dept/cs",
                      "/dept/career",
                    开发者_如何转开发  "/dept/it",
                      "/dept/mkt/",
                      "/dept/Membership/"
                      , "/dept/pr"
                      , "/dept/PS"
                      , "/dept/ta"
                      , "/dept/wc"
                      , "/dept/PubsProducts"
                      , "_layouts/settings.aspx"
                      ,"/_catalogs/masterpage/"];

       //If document.location.href = "http://localhost/dept/ce/default.aspx" 
       //And Array has "/dept/ce" values
       //Now check document.location.href against array values, if    
      //document.location.href contains array values then pass condition otherwise 
      //else condition 
    });

How to do this?


I believe you want to do something, IF the current location URL contains any of the strings in your array. Since you are using jQuery already, I recommend jQuery.grep.

Example:

if ($.grep(urls, function(str) { return location.href.indexOf(str) > -1; }).length > 0) {
  // do one thing
} else {
  // do another thing
}

Link: http://api.jquery.com/jQuery.grep/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜