admin管理员组

文章数量:1390399

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?

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?

Share Improve this question asked Dec 15, 2017 at 22:41 cfoster5cfoster5 1,8365 gold badges30 silver badges45 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 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);

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

本文标签: javascriptHow can I pass a query string to windowlocationreplace()Stack Overflow