【发布时间】:2022-01-22 04:25:50
【问题描述】:
为了避免重复网页,我正在寻找一种方法来根据页面的相对链接更改按钮中的链接。
假设我们在网站https://mywebsite.com/,我们想将用户发送到https://othersite.com/(这些网站作为示例给出
这是我想做的:
- 如果当前链接是https://mywebsite.com/,则按钮应提供此链接 https//othersite.com/
- 如果当前链接是https://mywebsite.com/?utm_source=facebook,那么同一个按钮必须提出这个链接https://othersite.com/?value=facebook
- 如果当前链接是https://mywebsite.com/?utm_source=twitter,那么同一个按钮必须提出这个链接https://othersite.com/?value=twitter
- 等
简而言之,我正在寻找一种方法来根据页面的当前链接来改变 href 属性中按钮的链接。
我刚开始使用 javascript。我远未理解这种语言的所有微妙之处。
这是我尝试过的,但它(显然)不起作用:
<html>
<head></head>
<body>
<a href="javascript:location.assign" >Click here</a>
<script>
if (location.pathname === "/") {
location.assign("https//othersite.com/");
}
if (location.pathname === "/?utm_source=facebook") {
location.assign("https://othersite.com/?value=facebook");
}
if (location.pathname === "/?utm_source=twitter") {
location.assign("https://othersite.com/?value=twitter");
}
</script>
</body>
</html>
这里还有一些我认为有用但无法解决我的问题的资源:
- Change href based on condition of URL on current page
- How to change href of <a> tag on button click through javascript
- Change part of link with part of current URL
感谢您的帮助和时间!
【问题讨论】:
标签: javascript href relative-url