javascript - 从值数组中查找 URL 字符串?

标签 javascript jquery

我正在尝试在 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/

相关文章:

javascript - 将 header 脚本传递给 AJAX 加载的内容

javascript - d3py 示例代码中出现 IO 错误 - 没有这样的文件或目录 : 'static/d3.js'

javascript - 在 jQuery 插件设置中使用特殊字符?

javascript - 变量和数学运算符

javascript - 如何在滚动到页面或 div 底部时运行函数?

jquery - 使用 jQuery Datatable,如何使搜索/条目等位于同一行

jquery 跷跷板效果

javascript - 根据与 `<a>` 类名的比较,将类添加到 `<li>` 标签

javascript - 有什么方法可以监听 Angular 变化检测完成事件

javascript - 如何将JavaScript函数下载的文件数据分配给保存的文件