我正在尝试创建一个javascript,使<a>
转到另一个链接,而不是在其href中指定的链接。例如,我想要
<a href="http://www.example.com/some-page">
改为转到:
http://www.newexamplesite.com/http:%252F%252Fwww.example.com%252Fsome-page
不幸的是,我没有访问此应用程序的jQuery的权限。必须是纯javascript。
我正在寻找一些可以包含在实际<a>
标签中的javascript:
<a onclick="" href="http://www.example.com/some-page">
请注意,必须检测原始链路,并且需要所有
/
用%252F
替代
试试这个:
<a onclick="window.location.href = 'http://newlink.com/' + encodeURIComponent(this.href); return false;" href="http://www.example.com/some-page/">Go to new link</a>
尝试以下操作:
<a onclick="event.preventDefault();location.href='http://www.uol.com.br/'" href="http://www.example.com/some-page">Link Text</a>
event.preventDefault
应该停止锚元素的默认行为(即,将用户引导到a href
.内的站点
EDIT:函数调用时忘记了括号!
您应该阻止默认事件(即转到href指定的链接)并转到另一个链接。
<a onclick="event.preventDefault();location.href='http://www.newexamplesite.com/'+this.href.replace(///g,'%252F')"
href="http://www.example.com/some-page">
test
</a>
您可以将href URL替换为如下函数调用:
<a href="javascript:goToLink()" />
<script>
function goToLink(){
// your link selection logic goes here
.....
// when you have selected the link you want to go to assign it to the window location.href attribute and the browser will redirect the user to the specified link
window.location.href = "..selected link...";
}
</script>