检查链接是指外部网站

时间:2017-02-25 10:02:12

标签: jquery

如果您看到类似问题,请提前致歉。我已经尝试查找我需要帮助转换此代码的内容,但收效甚微,而且已经很晚了。

我试图弄清楚如何改变:

 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>

1 个答案:

答案 0 :(得分:0)

我能够用这段代码找出答案。它基本上允许包含域名(&#39; localhost&#39;)的所有链接在我的情况下通过(&#39; .not&#39;)事件忽略代码。脚本的下一部分在链接上启动延迟计时器之前加载覆盖。

        series: [{
            innerSize: '30%',
            data: [
                {name: 'Yellow Slice', y: 12, color: 'yellow'},
                {name: 'Red Slice', y: 10, color: 'red' },
                {name: 'Blue Slice', y: 33, color: 'blue'},
                {name: 'Green Slice', y: 20, color: 'green'}
            ]
        }]