2013-03-16 1 views
0

어제 나는 비슷한 질문을했기 때문에 모든 외부 링크를 내 홈페이지로 리디렉션하는 방법을 알게되었습니다.리디렉션을 원하지 않는 경우를 제외하고 모든 외부 링크 리디렉션

// 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'; 
     } 
    }); 
}); 

하지만 지금 내가 facebook.com & twitter.com처럼, 내 홈 페이지로 리디렉션하지 않으려는 모든 링크를 제외 할, 내가 링크의 또 다른 조건을 만들려고하는 I 돈 리디렉션하고 싶지 않지만 작동하지 않습니다. 그런 다음 indexOf()text()으로 변경하려고했으나 여전히 작동하지 않습니다.


는 여기에 내가 노력이 코딩,하지만 난

// 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'; 
     } 
    }); 
}); 

또 다른 하나는 실패

// 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'; 
     } 
    }); 
}); 

그리고 모든 유사한 가능성 ...하지만 여전히 나를 위해 작동하지합니다.

답변

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. 
} 

희망이 도움이.

+0

작동하지 않습니다. : ( –

0

이 작업을 시도 할 수 있습니다 :
를 자바 스크립트

$(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]; 
    } 
    }); 
}); 

working example에서 당신은 HREF가 변경 볼 수있는 앵커 태그를 검사 할 수 있습니다.
나는 이것이 당신이 찾고있는 것이기를 바랍니다.

+0

kownleg.com, facebook & twitter 이외의 링크를 넣어 두지 않았 으면 좋겠어요. 내가 만든 U.Url 스크립트에 blogger.com을 넣었지만 작동하지 않습니다 ... check 그것 http://plnkr.co/edit/aAYuiXQadmIukpFIwoT0 –

+0

항상 동일한 색인에있는 scripts.js 파일에 허용 된 URL을 두 배열에 추가해야합니다. [확인] (http : // plnkr. 공동 편집/2NBAr2ZmgtvNKoMzvkyt? p = 미리보기). –

관련 문제