0

I would like to redirect the user but first I need to manipulate the url by changing a string:

I Do it like this:

var url = location.href;        
url=url.replace("featured", "list");    
window.location.href=url;

But before I redirect the user, I also would like to change a get parameter of the url to a specific value. I read around here that it should be done like this:

//
location.search = jQuery.query.set("page", 1);
//

I just don't know how to combine both the string and the parameter manipulation before redirecting because the location search doesn't take my string replacement into account and because the string replacement doesn't take the location.search into account neither.

Basically, if the user is here: http://www.mysite.com/index.php?page=3&mode=featured

I would like him to be redirected to: http://www.mysite.com/index.php?page=1&mode=list

And this, whatever the page number he's in of course.

PS: I'm not trying to force the user to anything. My point is to use a data filtering system and if it's activated, the user should be in list mode and start from page 1. That's all.

Any help would be appreciated.

8
  • Why not just use replace() again? Commented Mar 3, 2014 at 3:54
  • Because, as I said: "And this, whatever the page number he's in of course." How can I replace a value that I don't know yet? "Featured" can be replaced easily by "list" but there are an infinity of pages values possible and whatever the value, it should be replaced by 1. Commented Mar 3, 2014 at 3:56
  • How about taking every part of the text except for that number then? And reconstruct the URL. Commented Mar 3, 2014 at 3:58
  • Because there are other get variables in the url. My example is made to be as simple as possible to explain the situation but, in fact, the url can be as complex as it can get. The only thing I can rely on is that I may have a "featured" value (if it's the case, I change it) and I always have a page number parameter (whatever it is, its value should be changed to 1). The rest of the url is not predictable and should remain the same. Commented Mar 3, 2014 at 4:01
  • 1
    Split the string then? First based on ? so the second string is your list of parameters and then split that based on & and filter through them until you find one that .contains() "page" or something then manipulate it, and reconstruct your URL. Sorry, I'm not much help, I know this suggestion seems super roundabout and annoying. Commented Mar 3, 2014 at 4:05

2 Answers 2

1

You can run a GUP function. The GUP function gets a specific parameter in the url. In your case 'page' and 'model'

function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
  var regexS = "[\\?&]"+name+"=([^&#]*)";  
  var regex = new RegExp( regexS );  
  var results = regex.exec( window.location.href ); 
  if( results == null ){    
      return "";  
  }else{    
      return results[1];
  }
}
//this function checks the value of the 'page' and 'mode' parameters
function checkURL(){
    page = gup('page');
    mode = gup('mode');
    //if the page parameter is 1 and the mode parameter is 'featured' then redirect
    if(page == 3 && mode == 'featured){
        window.location.replace('http://www.mysite.com/index.php?page=1&mode=list');
    }
}

Hope this leads you in a right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

The final url is unknown but you assume it is. And the condition is irrelevant as I apply the changes anyway. Let's say the url can be of any form, it's not predictable but it always will have a mode and a page number. I need to output the exact same url (whatever it is) except with a 'list' mode and on page 1. Your script doesn't solve this issue.
0

Thank you coder29. It was the right direction even if the end was not what I needed.

This is the working script:

var regexS = "[\\?&]page=([^&#]*)";  
var regex = new RegExp( regexS ); 
var url = location.href;
var results = regex.exec( url ); 
if( results != null )
{    
    url=url.replace("featured", "list");
    url=url.replace("page="+results[1], "page=1");
}
window.location.href=url;

That is working like a charm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.