2017-02-25 5 views
0

似たような質問があった場合は、謝罪してください。私はこのコードを変換するのに必要なものを探してみましたが、ほとんど影響はなく、それは遅いです。チェックリンクは外部サイトを参照しています

私は変更する方法を把握しようとしている:

if (link.substr(0, 4) == 'http://localhost','https://localhost') 
    // If the link starts with http, assume external link. 
    // In that case, block normal behaviour, insert custom content and navigate after 5 seconds. 
    event.preventDefault(); 

を基本的にはその逆は、スクリプトの残りの部分を受け取るためにはlocalhostで始まらない任意のリンクを作ることに。リンクはすべて一緒にスクリプトを無視しますが。 if、else、returnステートメントを使用しようとしましたが、何が間違っているのか分かりません。助けてくれてありがとう!

 <script type="text/javascript"> 
     // Capture all link clicks. 
$('a').on('click', function(event) { 

    // Get the element and its href attribute. 
    var $element = $(event.target); 
    var link = $element.attr('href'); 

    if (link.substr(0, 4) == 'http://localhost','https://localhost') 
    // If the link starts with http, assume external link. 
    // In that case, block normal behaviour, insert custom content and navigate after 5 seconds. 
    event.preventDefault(); 

    $element.html("Leaving this website in 5..."); 

    setTimeout(function() { 
     console.log("This is when the redirect would take place, if not in Staack Snippets"); 
     document.location.href = link; 
    }, 5000); 
    } 
}); 
    </script> 

答えて

0

私はこのコードで答えを見つけ出すことができました。私の場合、ドメイン名( 'localhost')を含むすべてのリンクは、( '.not')イベント経由のコードを無視することができます。スクリプトの次の部分では、リンク上で遅延タイマーを開始する前にオーバーレイを読み込みます。

$('a[href^="http://"],a[href^="https://"]') 
     .not('[href*="localhost"]') 
     .click(function(e) { 
     e.preventDefault(); 
     var goTo = this.getAttribute("href"); 

     $(function() { 

     var loading = function() { 
      var over = '<div id="overlay">' + 
       '<img id="loading" src="/">' + 
       '</div>'; 
      $(over).appendTo('body'); 
     }; 
     loading() 
     }); 
     setTimeout(function(){ 

      window.location = goTo; 
     },5000);  
    }); 
関連する問題