如何使用javascript更改所有链接 [英] How to Change All Links with javascript
本文介绍了如何使用javascript更改所有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改网站的所有链接.假设.Example提供的链接 http://www.google.com/更改为
I want to change all links of my site. Suppose a link given by .Example http://www.google.com/ changes to http://www.mysite.com/?redirect=http://www.google.com/
我有我自己的重定向器,只是我需要通过javascript更改所有网址的链接
i have my own redirector just i need to change the links via javascript for all url
推荐答案
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = "http://www.mysite.com/?redirect=" + anchors[i].href
}
然后,您可以通过将代码包装到链接到window.onload事件的函数中,使代码在页面加载时运行:
You can then make the code run on page-load by wrapping it in a function linked to window.onload event:
window.onload = function() {
/* onload code */
}
这篇关于如何使用javascript更改所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文