简体   繁体   English

使用JavaScript在URL中发送/接收变量

[英]sending/receiving variable in URL with javascript

i want to send a url variable to 2nd page that contains links to a 3rd page. 我想将网址变量发送到包含指向第三页链接的第二页。 I want the 2nd page links to pickup the variable and append it to the link on that page. 我希望第二页链接能够提取变量并将其附加到该页面上的链接。 Then, when the user clicks on the link in that page the user is sent to the 3rd page carrying the variable along with it. 然后,当用户单击该页面中的链接时,该用户将被带到带有该变量的第三页。

page 1 = a href="http://www.page1.html?aff=1234" 第1页= a href =“ http://www.page1.html?aff = 1234”

page 2 = a href="http://www.page3?aff=????" 第2页= a href =“ http://www.page3?aff = ????”

so i need to have a script that will replace the ???? 所以我需要一个脚本来替换???? with "1234" 与“ 1234”

I can't use any server scripting- 我不能使用任何服务器脚本-

appreciate any help 感谢任何帮助

You can access them using location.search . 您可以使用location.search访问它们。

Check out the very popular solution here: 在此处查看非常流行的解决方案:

JavaScript query string JavaScript查询字串

You can easily switch pages by setting window.location . 您可以通过设置window.location轻松地切换页面。 You can include query strings there which will be passed to the second page. 您可以在其中包含查询字符串,这些查询字符串将传递到第二页。 For the target page, you can read location.search and extract the variable you want using the function provided in the link above. 对于目标页面,您可以阅读location.search并使用上面链接中提供的功能提取所需的变量。

On the 2nd page: 在第二页上:

<a href="http://www.page3" id="aff">...</a>

and in its script: 并在其脚本中:

document.getElementById('aff').href += location.search;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM