如何使用javascript更改所有链接

use*_*599 9 javascript url redirect hyperlink

我想更改我网站的所有链接.假设.Example http://www.google.com/提供的链接更改为http://www.mysite.com/?redirect=http://www.google.com/

我有自己的重定向器,我需要通过javascript更改所有网址的链接

Bea*_*rdy 23

var anchors = document.getElementsByTagName("a");

for (var i = 0; i < anchors.length; i++) {
    anchors[i].href = "http://www.mysite.com/?redirect=" + anchors[i].href
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过将代码包装到链接到window.onload事件的函数中来使代码在页面加载上运行:

window.onload = function() {
       /* onload code */
}
Run Code Online (Sandbox Code Playgroud)

  • 100% 工作正常!谢谢!堆栈溢出太棒了! (2认同)