I have some url strings ending in
&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
I have a variable defined
var liveScoringWeek = 18;
I need to find the url string if it matches the following. Week18 being the variable that changes week to week.
&O=07
&O=07&WEEK=18
I tried this and doesn't work
if (window.location.href.indexOf("&WEEK=") === liveScoringWeek && window.location.href.indexOf("WEEK=") < 0) {
alert("found it");
}
Heres an example where you can get either of the patterns you want, we check for an exact match and then use the ||
option to create an OR command and check for a lack of the string &WEEK=
.
Conditions:
// 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)
If statement with OR functionality:
// If function requiring either or of two conditions.
// Replace CONDITION_1 with a true/false qualifier
if ( CONDITION_1 || CONDITION_2 ) {
...
}
Let me know if you were hoping for something else.
// 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>
You could try the includes
-function
Try this:
let currentUrl = window.location.href;
if(currentUrl.includes("WEEK="+liveScoringWeek)){
console.log("found it!");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.