2

I need to find the way to keep the parameters in the url upon navigation if they are entered once, ie: ?aff=john

So for example, user comes to website.com/?aff=john and navigates to about-us I need to make that url parameters are kept, so the full website name is: website.com/about-us/?aff=john

This is what I've tried so far, but it is not working.. it keeps adding the url parameters (window.location.search)

var params = false
var baseUrl = ''
var currUrl = window.location.href

if (window.location.search != '') {
  params = true
}

if (params) {
  baseUrl = currUrl + window.location.search
  window.location.href = baseUrl
}

Thanks.

EDIT: already tried proposed.. not working.

  • 2
    This sounds like a real pain to maintain. Why not use a session variable, or cookie to store the value when set by the first page? – Rory McCrossan Oct 03 '17 at 15:38
  • Possible duplicate of [Persistent URL query string throughout the entire site?](https://stackoverflow.com/questions/18214497/persistent-url-query-string-throughout-the-entire-site) – DaFois Oct 03 '17 at 15:38
  • @RoryMcCrossan How can I interpolate it then in the `location.href`? –  Oct 03 '17 at 15:39
  • You can use a javascript to grab the query string, put it into a cookie then add the cookie string to every href of your document. – DaFois Oct 03 '17 at 15:41
  • @DanieleFois Can you please post the working solution? –  Oct 03 '17 at 15:46
  • first try by yourself... when you get stuck you'll surely find somebody who will help you – DaFois Oct 03 '17 at 15:51
  • I didi try. So you only copied answer from here: https://stackoverflow.com/questions/18214497/persistent-url-query-string-throughout-the-entire-site but the truth is you don't know as well.. –  Oct 03 '17 at 17:48

3 Answers3

0

You can save the query string using window.location.search and then you can add link handler in using jQuery like below:

var glString = window.location.search;
$('a').on('click', function(evt) {
    evt.preventDefault();
    window.location = $(this).attr('href') + glString;
});

or if you prefer javascript

var glString = window.location.search;
var links = document.getElementByTagName('a');
links.addEventListener('click', function(evt) {
    evt.preventDefault();
    window.location = $(this).attr('href') + glString;
});
Tapas
  • 1,123
  • 8
  • 16
0

You can use sessionStorage to save navigation data into a key. Pick it up whenever required. Now-a-days, all browsers support it except Opera mini.

Hopefully, your software does not have browser constraints and your application does not have to work on outdated browsers.

As copied from mozilla site, code to use sessionstorage would be like :

    // Save data to sessionStorage
    sessionStorage.setItem('key', 'value'); 

    // Get saved data from sessionStorage 
     var data = sessionStorage.getItem('key'); 

    // Remove a key from sessionStorage
    sessionStorage.removeItem('key'); 

    // Remove all data from sessionStorage 
     sessionStorage.clear();

This way, you won't need to append it on every page. For the domain url and in current browser session, you can get it from sessionStorage.

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
0

You can temporary save the previous url using sessionStorage.

sessionStorage.setItem('parameter', 'dataString'); sessionStorage.getItem("parameter");

Surajit
  • 89
  • 5