For example, I have: http://A.com/a/index.html, the page has a lot of different web sites outside the chain (such as B.Com, c.com, d.com, etc.).
Now I think, with PHP, JS or other methods to achieve, click the outside link "b.com", the new window opens the address is "http://a.com/b/index.php?b.com" effect.
Thank you, because I do not understand the code, I hope that the big enlighten, how to replace all the form of the outside chain?
Reply content:
For example, I have: http://A.com/a/index.html, the page has a lot of different web sites outside the chain (such as B.Com, c.com, d.com, etc.).
Now I think, with PHP, JS or other methods to achieve, click the outside link "b.com", the new window opens the address is "http://a.com/b/index.php?b.com" effect.
Thank you, because I do not understand the code, I hope that the big enlighten, how to replace all the form of the outside chain?
click
the event is captured and then modified in the event handler function href
.
$(document).on('click', 'a:not([data-bypass])', function(e) { var $target = $(e.target), href = $target.attr('href'), prefix = 'http://a.com/b/index.php?'; if (/^http:\/\//.test(href)) { $target.attr('href', prefix + href); } // 把检查过的 标记一下,以后就不会再次做检查 $target.attr('data-bypass', 'bypass');});
This idea is compared with the idea of direct traversal and
modification, the href
advantage is that there is no dead angle and high efficiency, which is still effective for the later addition
. And for special handling needs
, it is convenient to write the attribute on the label first data-bypass
.
jquery gets all A tags, and then the bulk replaces href
the value as the http://
beginning of the
$("a[href*='http://']").each(function() { $(this).attr('href','http://www.fsdeveloper.net'+'/'+$(this).attr('href'));});
Similar to this notation,