Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add / Change parameter of URL and redirect to the new URL

Tags:

If the &view-all parameter does NOT exist in the URL I need to add it to the end of the URL along with a value. If it DOES exist then I need to be able to just change the value without creating a new URL because it might, or might not, have other parameters before it.

I found this function but I can't get it to work: https://stackoverflow.com/a/10997390/837705

Here is the code I have using the function above (which I can't get to work): http://jsfiddle.net/Draven/tTPYL/4/

I know how to append the parameter and value already:

<div onclick="javascript: window.location.assign(window.location.href+='&view-all=Yes');">Blah Blah</div>

More Info:

If the URL is http://www.domain.com/index.php?action=my_action then the default "&view-all" value would be "Yes" so the URL they would be directed to when they click the button is http://www.domain.com/index.php?action=my_action&view-all=Yes.

If the URL is http://www.domain.com/index.php?action=my_action&view-all=Yes then when they click the button it would change to http://www.domain.com/index.php?action=my_action&view-all=No​​​

EDIT: Please give me examples. I don't know alot of JS, and I just can't think of a way to do it in PHP.

like image 315
Draven Avatar asked Oct 25 '12 07:10

Draven


People also ask

Can you redirect URLs with parameters?

Redirect with parameters to a specific page/folder/subfolder Subfolders you are specifying in the address bar (e.g., /example) will be placed before the subfolders specified in destination URL (/page). All the parameters are combined and placed at the end of the URL. NOTE: The URL redirect from domain.

How do I redirect a URL to another URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do you update parameters in a URL?

The value of a parameter can be updated with the set() method of URLSearchParams object. After setting the new value you can get the new query string with the toString() method. This query string can be set as the new value of the search property of the URL object.


1 Answers

function setGetParameter(paramName, paramValue) {     var url = window.location.href;     var hash = location.hash;     url = url.replace(hash, '');     if (url.indexOf(paramName + "=") >= 0)     {         var prefix = url.substring(0, url.indexOf(paramName + "="));          var suffix = url.substring(url.indexOf(paramName + "="));         suffix = suffix.substring(suffix.indexOf("=") + 1);         suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";         url = prefix + paramName + "=" + paramValue + suffix;     }     else     {     if (url.indexOf("?") < 0)         url += "?" + paramName + "=" + paramValue;     else         url += "&" + paramName + "=" + paramValue;     }     window.location.href = url + hash; } 

Call the function above in your onclick event.

like image 200
Lajos Arpad Avatar answered Oct 14 '22 21:10

Lajos Arpad