0

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");
}
MShack
  • 642
  • 1
  • 14
  • 33
  • Does this answer your question? [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – Rojo Jan 14 '21 at 18:41
  • to redirect to the current week url? – Aalexander Jan 14 '21 at 18:43
  • Assuming `liveScoringWeek` contains the week number in question, I think you should only need to check `if (window.location.href.indexOf("&WEEK=" + liveScoringWeek) > 0)` – critical_error Jan 14 '21 at 18:44

2 Answers2

1

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>
Oliver Trampleasure
  • 3,293
  • 1
  • 10
  • 25
0

You could try the includes-function

Try this:

let currentUrl = window.location.href;
if(currentUrl.includes("WEEK="+liveScoringWeek)){
    console.log("found it!");
}
Christoph Kern
  • 419
  • 2
  • 7