0

Sample URL:

.com/projects.php?&filterDate=this_week?page=5


The query strings like I've listed above may or may not have the ?page=5 query string in them. I'm looking for a way to grab the URL (done), search the string to determine whether or not it has the ?page=# query string (also done), add it in if it's not there (also done), but if it is there, replace it with a different number (need help with this). The code currently doesn't change the query string (ie page=5 doesn't change to page=6 or anything else for that matter). It doesn't seem like the .replace method's regex is correct (see current_window_location3 variable) below.

//Get the current URL
var current_window_location = window.location.href;

if(current_window_location.match("\\?page=([^&]+)")){
    //Replace this query string 
    var current_window_location3 = current_window_location.replace("\\?page=([^&]+)", new_page_num);

    //Go to this newly replaced location
    window.location = current_window_location3;

}else{
    //Add clicked '?page=#' query string to the URL
    var current_window_location2 = current_window_location + "?page="+new_page_num;

    //Go to this new location
    window.location = current_window_location2;
}
FiringBlanks
  • 1,998
  • 4
  • 32
  • 48
  • You're using replace with the **string** `"\\?page=([^&]+)"`. Try it with the **regex** `/\\?page=([^&]+)/`. – SamWhan Sep 06 '17 at 06:35
  • `.replace(/\?page=(\d+)/, (m, num) => '?page=' + (Number(num) + 1))`. – Tushar Sep 06 '17 at 06:41
  • Don't use regexp to parse and manipulate URLs, including query strings. Use a library. The emerging standard is [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL). By the way, this URL is malformed (it uses `?` and `&` incorrectly. You should fix that before you do anything else. –  Sep 06 '17 at 06:46
  • @torazaburo I try again - how is this a duplicate of https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript? OP's problem here is the confusion of string and regex. – SamWhan Sep 06 '17 at 13:17

1 Answers1

0

String.prototype.replace() takes as its first value, the search pattern, a string or a regex. If a string ("abc?") is given, it searches for the literal string to replace. If a regex (/abc?/) is passed a regex match, a search is done using regular expressions.

Change your

var current_window_location3 = current_window_location.replace("\\?page=([^&]+)", new_page_num);

to

var current_window_location3 = current_window_location.replace(/\?page=([^&]+)/, new_page_num);

Here's an illustrating snippet:

var current_window_location = '.com/projects.php?&filterDate=this_week*?page=5*',
    window_location = '',
    new_page_num = 123;

if(current_window_location.match("\\?page=([^&]+)")){
    //Replace this query string 
    var current_window_location = current_window_location.replace(/\?page=([^&]+)/, new_page_num);

    //Go to this newly replaced location
    window_location = current_window_location;

} else {
    //Add clicked '?page=#' query string to the URL
    var current_window_location = current_window_location + "?page="+new_page_num;

    //Go to this new location
    window_location = current_window_location;
}

document.write("window_location = " + window_location);
SamWhan
  • 8,296
  • 1
  • 18
  • 45
  • _The code currently doesn't change the query string (ie page=5 doesn't change to page=6_ – Tushar Sep 06 '17 at 06:45
  • @Tushar Yes...? I think the number `6` was just an example in this case. In OP's example `new_page_num` is undefined. I choose to set it `123` because it's a more visible change. If you set it to `6` you'll get your desired output :) – SamWhan Sep 06 '17 at 07:20
  • @torazaburo You're right. I fixed it in the "answer", but missed it in the code :S One question though, how is this a duplicate of https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript? OP's problem here is the confusion of string and regex. – SamWhan Sep 06 '17 at 07:24
  • I think OP wants to increment the `page` number by one. So, when it is 5, it should become 6. You're hard-coding the numbers. I'll let it to the OP to clarify this. – Tushar Sep 06 '17 at 07:24
  • @Tushar If you check OP's code example it just sets `page` to be whatever is in the variable `new_page_num`. It doesn't read the current value and increment it. – SamWhan Sep 06 '17 at 07:27