jquery - How to apply javascript function to href with specific domains -



jquery - How to apply javascript function to href with specific domains -

i need add together redirect other links in website, utilize

window.onload = function() { var anchors = document.getelementsbytagname("a"); (var = 0; < anchors.length; i++) { anchors[i].href = "http://shorter.com/?redirect=" + anchors[i].href } }

this add together links, need specify domains redirect them like:

var domains = ['depositfiles.com', 'rapidshare.com'];

edit

for illustration page has many links (file.com, depositfiles.com, inda.com). need href domains in "domain" variable redirected using redirector while other kept same (direct).

like depostitfiles.com http://shorter.com/?redirect=http://depositfiles.com/

for inda.com http://inda.com

just utilize hostname matching:

class="snippet-code-js lang-js prettyprint-override">var domains = ["google.com", "stackoverflow.com"]; window.onload = function() { var anchors = document.getelementsbytagname("a"); (var = 0; < anchors.length; i++) { for(var j = 0; j < domains.length; j++){ if(anchors[i].hostname === domains[j]){ //domain nowadays anchors[i].href = "http://shorter.com/?redirect=" + anchors[i].href break; } } } }

good point @jonathan-gray - edited

javascript jquery url href

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -