Alex Rivera | Logout

How to Change All Links with javascript

Asked 2012-11-08T14:46:41.533
14

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/

i have my own redirector just i need to change the links via javascript for all url

Edit
Report

1 Answer

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

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

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 */
}
answered 2012-11-08T14:59:24.150

Your Answer