-4

SHORT: Need a script to remove the parameters on URL IF EXIST. Must be jquery/Script/

LONG: I have a site where I send traffic to with lots of parameters. These are processed instantly with php to cookies. After my cookie set I would like to refresh the same page but WITHOUT parameters. I cannot do it php since the cookies need to be processed by an iframe so I need a jquery/script to do it AFTER cookies are set via the iframe.

Thanks guys! :)

Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
Eric
  • 1
  • 2

3 Answers3

1

The below code will remove the query string parameter and will reload the page

window.location.href = window.location.protocol+"//"+window.location.hostname+window.location.pathname
asifsid88
  • 4,631
  • 20
  • 30
  • It does but since it is same domain/path it keeps refreshing in a loop. Thanks anyway. :) – Eric Feb 06 '13 at 16:21
0

The search attribute of window location describes the GET parameters on the url so -

if (window.location.search != "") {
    window.location.search = "";
}

You may want to check out this question for a another solution.

And have a look at the MDN Documentation on window.location for more info.

Community
  • 1
  • 1
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
-2

I think you need to use something like:

var url_update= false;
var stripped_url;
function removeparametersFromURL(url_string) {
if (url_string.indexOf("#") > 1) {
    URL = url_string.split("#")[0];
    url_update= true;
}
if (url_string.indexOf("?") > 1) {
    URL = url_string.split("?")[0];
    url_update= true;
}
return URL;
}

var stripped_url = removeparametersFromURL(top.location.href);
if(url_update){
top.location.href = stripped_url;
}

setTimeout(function(){
stripped_url = removeparametersFromURL(top.location.href);
if(url_update){
top.location.href = stripped_url;
}
},5000); //will call function after 5 seconds
Sankar V
  • 4,110
  • 5
  • 28
  • 52
  • This does the work instantly. Just 1 problem. It is too fast and my PHP script do not have time to get the parameters and insert them in cookies. I put your script ate the very bottom of my footer and no luck. Can we had a delay? Thanks so much! – Eric Feb 06 '13 at 16:25
  • I see what is happening. Cookies are set with parameters but when it redirects (with your script) they are deleted since the php says – Eric Feb 06 '13 at 18:00
  • The above script does nothing with the cookies that you have set. It does what you have asked for in this post. Please accept the answer and upvote if it helped you. – Sankar V Feb 06 '13 at 18:04
  • Those who are downvoting my (accepted) answer please post a better solution also! I applied a simple logic and updated according to his comments. Also I know it is not perfect. – Sankar V Feb 28 '13 at 07:44