最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Using javascript navigate to a page and also send querystring? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jan 26, 2015 at 17:30 mustafa mukadammustafa mukadam 731 silver badge8 bronze badges 8
  • Basically, I want to send data between html pages without using server-side scripting. – mustafa mukadam Commented Jan 26, 2015 at 17:32
  • I just tried your code on a chrome console and it did what you are asking for – bitoiu 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? – atmd Commented Jan 26, 2015 at 17:36
  • 1 try window.location = 'page2.html?uname=mustafa'; – charlietfl Commented Jan 26, 2015 at 17:42
  • 2 It turns out there is some error in other part of code. Above solution worked! – mustafa mukadam Commented Jan 26, 2015 at 18:09
 |  Show 3 more ments

3 Answers 3

Reset to default 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./?test=blah&foo=bar';
window.location.href = 'http://google./?test=blah&foo=bar';

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("&");

try this

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

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

发布评论

评论列表(0)

  1. 暂无评论