0

Yesterday I asked the similar question, so I get to know how to redirects all the external links to my home page.

// when the document is ready
$(document).ready(function() {
    // iterate over all the anchor tags
    $("a").each(function() {
        // if the current link's href doesn't already contain 'www.kownleg.com'
        if (this.href.indexOf('www.kownleg.com') === -1) {
            // change current link to home page
            this.href = 'http://www.kownleg.com';
        }
    });
});

But now I want to exclude all the links which I don't want to redirect to my home page, like facebook.com & twitter.com, I tried to make another condition for the links which I don't want to redirect, but its not working. Then I tried to change indexOf() with text() but still not working.


Here is two codings which I tried, but I failed

// when the document is ready
$(document).ready(function() {
    // iterate over all the anchor tags
    $("a").each(function() {
        // if the current link's href doesn't already contain 'www.kownleg.com'
        if (this.href.indexOf('www.kownleg.com') === -1) {
            // change current link to home page
            this.href = 'http://www.kownleg.com';
        }
        if (this.href.indexOf('www.facebook.com') === -1) {
            // change current link to home page
            this.href = 'http://www.facebook.com';
        }
        if (this.href.indexOf('www.twitter.com') === -1) {
            // change current link to home page
            this.href = 'http://www.twitter.com';
        }
    });
});

Another One:

// when the document is ready
$(document).ready(function() {
    // iterate over all the anchor tags
    $("a").each(function() {
        // if the current link's href doesn't already contain 'www.kownleg.com'
        if (this.href.indexOf('www.kownleg.com') === -1) {
            // change current link to home page
            this.href = 'http://www.kownleg.com';
        }
        if (this.href.text('www.facebook.com') === -1) {
            // change current link to home page
            this.href = 'http://www.facebook.com';
        }
        if (this.href.text('www.twitter.com') === -1) {
            // change current link to home page
            this.href = 'http://www.twitter.com';
        }
    });
});

And all the similar possibilities... but still not work for me.

4

2 回答 2

0

你可以试试这个:
在 Javascript

$(document).ready(function() {
  var allowed_links_array = [
    "www.kownleg.com",
    "www.twitter.com",
    "www.facebook.com"
  ];
  var replacement = [
    "http://www.kownleg.com",
    "http://www.twitter.com",
    "http://www.facebook.com"
  ];
  // iterate over all the anchor tags
  $("a").each(function() {
    var index;
    //get the index of the last part of the href attribute after the /
    index = jQuery.inArray(
      this.href.split('/')[this.href.split('/').length - 1],
      allowed_links_array
    );
    //if the link is in the allowed_links_array then set the href to the right link
    if(index !== -1){
      this.href = replacement[index];
    }
  });
});

工作示例,您可以检查锚标记以查看 href 更改。
我希望这就是你要找的。

于 2013-03-16T21:43:30.820 回答
0

您需要将其全部包裹在 if 语句中:

if(
    (this.href.indexOf('www.twitter.com')  !== -1) ||
    (this.href.indexOf('www.kownleg.com')  !== -1) ||
    (this.href.indexOf('www.facebook.com') !== -1)
  ){
  //do what you were doing.
}

希望这有帮助。

于 2013-03-16T16:48:31.800 回答