我有一些url字符串以
结尾&O=07
&O=07&WEEK=14
&O=07&WEEK=15
&O=07&WEEK=16
&O=07&WEEK=17
&O=07&WEEK=18
&O=07&WEEK=19
我定义了一个变量
var liveScoringWeek = 18;
我需要找到url字符串,如果它匹配以下内容。Week18为每周变化的变量
&O=07
&O=07&WEEK=18
我试过了,不工作
if (window.location.href.indexOf("&WEEK=") === liveScoringWeek && window.location.href.indexOf("WEEK=") < 0) {
alert("found it");
}
这里有一个示例,您可以获得您想要的模式中的任何一个,我们检查是否完全匹配,然后使用||
选项创建一个OR命令并检查是否缺少字符串&WEEK=
。
条件:
// Returns true if "&WEEK=18" is present in string currentURL
currentURL.includes("&WEEK=" + liveScoringWeek)
// Returns true if "&WEEK=" is NOT present in string currentURL, i.e. has an index of -1
currentURL.indexOf("&WEEK=") === -1)
带有OR功能的If语句:
// If function requiring either or of two conditions.
// Replace CONDITION_1 with a true/false qualifier
if ( CONDITION_1 || CONDITION_2 ) {
...
}
如果你想要别的东西,请告诉我。
// Store week variable
var liveScoringWeek = 18
// Check URL Function
function checkURL(currentURL) {
// Check if exact match with week
// Or no presence of &WEEK= in url
if (currentURL.includes("&WEEK=" + liveScoringWeek) || currentURL.indexOf("&WEEK=") === -1) {
// Prove we've found them
console.log("found it!");
}
}
// Add test function to buttons
$(".test").click(function() {
// Check URL taken from button text
checkURL($(this).text());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="test">test.com/?something&O=07</button>
<button class="test">test.com/?something&O=07&WEEK=18</button>
<button class="test">test.com/?something&O=07&WEEK=19</button>
您可以尝试includes
-function
试试这个:
let currentUrl = window.location.href;
if(currentUrl.includes("WEEK="+liveScoringWeek)){
console.log("found it!");
}