1

I got my query string in document. ready but then I want to remove it and save value in a global variable without refreshing the page, but it just never happens..

var globalVar = null;

$(document).ready(function () {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    results == null ? null : decodeURIComponent(results[1].replace(/\+/g, " "));

    if (results != null)
        globalVar = results[1];

     window.location.search = '';
});

This code keeps reloading page forever

Edit

Here is what is happening,

User gets email to a link, to select right row in table on the site, we are using querystring as using hash doesn't work (TMG server removes it), so once user gets on the site, we don't need query string anymore as we are using hash (because hash don't refresh page on change.)

Edit 2

Why don't want to keep query string and hash ?

because I can update hash without refresh whereas updating querystring refreshes the page :(, so i don't want it to be like

www.example.com/sites/fruitstore?fruitid=123#fruitid=432

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 1
    Why? What is the point of this? – Niet the Dark Absol Feb 27 '14 at 16:20
  • What exactly are you asking for? – Kevin Jantzer Feb 27 '14 at 16:20
  • Why would you want to remove the query string? FYI, you can do this with the [history API](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history), but there hardly is a good reason to do so. – Bergi Feb 27 '14 at 16:21
  • @NiettheDarkAbsol because I am using window.location.hash afterwords for finding the right row in two different but connected tables, query string is only when someone comes from outside – Mathematics Feb 27 '14 at 16:21
  • You have to use the history API for that. – Felix Kling Feb 27 '14 at 16:22
  • @CustomizedName: Why not let both query string and hash work? Also, don't reassign to `location` once the string is empty, to avoid the infinite reloading. – Bergi Feb 27 '14 at 16:22

5 Answers5

4

You need to use the history api instead of just setting the location to something new.

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

pushstate or replaceState will allow you to change the url displayed in the browser without a page refresh.

This script could also help you out: https://github.com/browserstate/history.js/

Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
0

I think your "reloading" problem comes from this line:

window.location.search = ''

This SO answer will give you the good method to handle browser URL history:

Community
  • 1
  • 1
dweeves
  • 5,525
  • 22
  • 28
0

you'll need the history API of modern browsers, to concrete, pushState will do it.

reference

and you'll probably want to use a library, as f.e. history.js, as handling different browsers can be very tricky...

benzkji
  • 1,718
  • 20
  • 41
0

Use the history API for this:

window.history.replaceState({}, document.title,
                            window.location.href.split('?')[0]);

replaceState replaces the current history entry with the given values. The first parameter is a data parameter, which is not used in this example. The second parameter is the window title. The final parameter is the new URL. The split takes everything before the beginning of the query parameters. If you need things that follow a #, then you'll have to use a more complicated URL re-building function.

replaceState does not add a new entry to history, which keeps you from filling the history with useless values.

See this answer for more information.

J David Smith
  • 4,780
  • 1
  • 19
  • 24
0
window.location.search = '';

This code keeps reloading page forever

Yeah, obviously. Instead, use

if (location.search != '')
    location.search = '';

once user gets on the site, we don't need query string anymore as we are using hash (because hash don't refresh page on change.)

You also might want to try the history API, so that you always have a copy-and-pastable querystring URL in the location bar.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375