我正在尝试在 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
}
关于javascript - 从值数组中查找 URL 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7488735/