捕获URL参数并在HREF链接中传递[重复]

时间:2022-06-05 14:15:56

This question already has an answer here:

这个问题在这里已有答案:

I need to capture web parameters of a URL. The URL looks like this:

我需要捕获URL的Web参数。 URL如下所示:

http://thisisurl.com?p=parameter

I need ?p=parameter to be passed on to all HREF links on the page, so if i have:

我需要将?p =参数传递给页面上的所有HREF链接,所以如果我有:

http://thisisurl2.com">link

I should land on http://thisisurl2.com?p=parameter

我应该登陆http://thisisurl2.com?p=parameter

Can I use JS to capture the parameter and pass it in as a variable in the HREF link?

我可以使用JS捕获参数并将其作为HREF链接中的变量传递吗?

1 个解决方案

#1


0  

Example:

var url = "http://thisisurl.com?p=parameter";
var urlQueryString = url.slice(url.indexOf("?"));//return value is ?p=parameter
var hrefLink = "http://thisisurl2.com" + urlQueryString;
document.getElementById("idoflink").href = hrefLink;
<a href="" id="idoflink">Example</a>

#1


0  

Example:

var url = "http://thisisurl.com?p=parameter";
var urlQueryString = url.slice(url.indexOf("?"));//return value is ?p=parameter
var hrefLink = "http://thisisurl2.com" + urlQueryString;
document.getElementById("idoflink").href = hrefLink;
<a href="" id="idoflink">Example</a>