4

I want to navigate from page1.html to page2.html and at the same time adding querystring in URL using javascript.

I tried:

window.location.href = 'page2.html?uname=mustafa';

It does navigate to page2.html but "?uname=mustafa" is not added in URL. So am unable to parse querystring on page2. How can I achieve it?

8
  • Basically, I want to send data between html pages without using server-side scripting. Commented Jan 26, 2015 at 17:32
  • I just tried your code on a chrome console and it did what you are asking for Commented Jan 26, 2015 at 17:35
  • 1
    what is your serverside set up? maybe you are using a cms or some sytem thats stripping the query string? Commented Jan 26, 2015 at 17:36
  • 1
    try window.location = 'page2.html?uname=mustafa'; Commented Jan 26, 2015 at 17:42
  • 2
    It turns out there is some error in other part of code. Above solution worked! Commented Jan 26, 2015 at 18:09

3 Answers 3

3

Have you tried using just

window.location = 'page2.html?uname=mustafa';

instead of using .href? This should preserve the querystring.

Worked for me in Chrome and FF with .href just fine, but I used absolute URLs:

window.location = 'http://google.com/?test=blah&foo=bar';
window.location.href = 'http://google.com/?test=blah&foo=bar';
Sign up to request clarification or add additional context in comments.

3 Comments

still not preserving :(
It turns out there is some error in other part of code. Above solution worked!
don't forget to mark the accepted answer, or create an answer for any solution you figured out yourself
2

I have ran into this problem before as well. I used location.href with an absolute URL and a parameters array like the follow:

var params = [];
params.push("uname=" +mustafa);
location.href = "http://localhost:port/page2.html?" + params.join("&");

Comments

0

try this

window.location = `addUser.html?userId=${userId}`;

here userId is a parameter and ${userId} is variable value

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.