我正在尝试在URL字符串中找到数组值?检查条件如果请求的URL包含数组值,则传递条件,否则将重定向到google.com



     $(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
    });


这该怎么做?

最佳答案

如果当前位置URL包含数组中的任何字符串,我相信您想做些什么。由于您已经在使用jQuery,因此我建议使用jQuery.grep。

例:

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


链接:http://api.jquery.com/jQuery.grep/

关于javascript - 从值数组中查找URL字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7488735/

10-09 17:02
查看更多