2017-02-25 40 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(); 

基本上它的反向建立不與本地主機開始的任何環節爲接收腳本的其餘部分。儘管一起忽略腳本的鏈接。我試圖使用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);  
    });