2017-02-25 99 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);  
    });