3

I am sending an email that links a user to a URL with a query string. I am retrieving this string with:

var getQueryString = function ( field, url ) {
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
};

var list = getQueryString('list', window.location.href);
console.log(list);

I want to pass this query string to another link on this page. My current function reads as:

function signin(){
  var email = document.getElementById('email').value;
  var password = document.getElementById('password').value;
  firebase.auth().signInWithEmailAndPassword(email, password).then(function(){
    window.location.replace("management.html" + list);
  })
  .catch(function(error) {
    ...
  });
}

How can I correctly pass the variable list to signin?

0

2 Answers 2

3

Your list variable only contains the value of the query '?list=123' which for example would be '123'

You aren't creating a new query string....just adding that same value to end of the new url so it would look like "management.html123'

If you want the whole query string from current page passed to new page you can use location.search

location.replace("management.html" + location.search);

Or for just the 'list' do:

location.replace("management.html?list=" + list);
1
  • Wow. Almost too easy.
    – cfoster5
    Dec 16, 2017 at 4:12
-1

If these are not on the same page and the global variable is not accessible, I would use the local storage solution presented here:

Passing Variable through JavaScript from one html page to another page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.