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.

Baylock
  • 1,246
  • 4
  • 25
  • 49
  • Why not just use `replace()` again? – domdomcodecode Mar 03 '14 at 03: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. – Baylock Mar 03 '14 at 03:56
  • How about taking every part of the text except for that number then? And reconstruct the URL. – domdomcodecode Mar 03 '14 at 03: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. – Baylock Mar 03 '14 at 04: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. – domdomcodecode Mar 03 '14 at 04:05
  • No problem. This was my first idea but it seems that jquery can manipulate url parameters and I believe that it could be achieved in a simpler way than isolating, evaluating and replacing strings when a value is unknown. The two scripts I took as an example are ok, I just would like to combine them somehow. If it's not possible then ok but if it is, it would be nice to know how. – Baylock Mar 03 '14 at 04:08
  • I'm not too well versed in jQuery as you can see, hehe. Found something that might help. http://stackoverflow.com/questions/7171099/how-to-replace-url-parameter-with-javascript-jquery – domdomcodecode Mar 03 '14 at 04:10
  • Thank you and no problem. As far as I know, the link explains how to make a text replacement from an src. That, I can do but I don't have any src to play with as I get the url from the current location. But if I get it, I manipulate a string while manipulating the url parameters is a total different thing. What I would like is to mix this two different things together. My question rephrased is this: how can I store in a variable the resulting url of the "jQuery.query.set()". – Baylock Mar 03 '14 at 04:18

2 Answers2

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.

coder29
  • 175
  • 6
  • 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. – Baylock Mar 03 '14 at 14:33
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.

Baylock
  • 1,246
  • 4
  • 25
  • 49