JavaScript-How do i modify all external link forms in an HTML page?

Source: Internet
Author: User
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?

clickthe 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,

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.