代码之家  ›  专栏  ›  技术社区  ›  mystupidstory

用于检查url是否存在的Javascript

  •  2
  • mystupidstory  · 技术社区  · 10 年前

    我用JavaScript编写了一个脚本,检查单击的链接是否有效 脚本基本上执行以下操作

    • 拦截所有链接点击
    • 检查它们是否嵌套在 a[href] 标签
    • 检查url中是否有特定文本,否则允许链接
    • 检查该URL是否有效

    如果URL 404s或其他内容,我希望阻止点击

    function iClicked(event) {
      var link = event.target;
      //go up the family tree until A tag
      while (link && link.tagName != 'A') {
        link = link.parentNode;
      }
    
      if (link) {
        var url = link.href;
        var ajaxurl = link.getAttribute('href');
        var needToCheck = url.indexOf('speed') != -1;
        //check if the url has the string 'speed' in it
        if (needToCheck) {
          var reader = new XMLHttpRequest();
          //asynchronous is true
          reader.open('get', ajaxurl, true);
          //check each time the ready state changes
          //to see if the object is ready
          reader.onreadystatechange = checkReadyState;
    
          function checkReadyState() {
            if (reader.readyState === 4) {
              //check to see whether request for the file failed or succeeded
              if ((reader.status == 200) || (reader.status === 0)) {
                //page exists - redirect to the clicked url
                document.location.href = url;
    
              } else {
                //if the url does not exist
                alert("No use going there!");
                return false;
              }
            }
          }
        }
      }
      return true;
    }
    
    //intercept link clicks
    document.onclick = iClicked;
    

    现在它不起作用了,我感觉有点不对劲 ajaxurl init和 reader.open 具有 半旋转 也许 return false 部分。但我还是看不清整件事。我对JavaScript很陌生,所以你们能帮我吗?

    编辑/结束问题 多亏了@Louy和@epascarello,代码才完整。

    // ==UserScript==
    // @name        Check before Click
    // @namespace   CheckbeforeClick
    // @include     *
    // @version     1
    // @grant       none
    // ==/UserScript==
    
    
    function iClicked(event) {
        var link = event.target;
        //go up the family tree until A tag
        while (link && link.tagName != 'A') {
            link = link.parentNode;
        }
    
        if (!link) return true;
    
        var url = link.href;
        var ajaxurl = link.getAttribute('href');
        //change the following to apply on other links, maybe regex
        var needToCheck = url.indexOf('speed') != -1;
        //check if the url has the string 'speed' in it
        if (!needToCheck) return true;
    
        var reader = new XMLHttpRequest();
        //asynchronous is true
        reader.open('get', ajaxurl, true);
        //check each time the ready state changes
        //to see if the object is ready
        reader.onreadystatechange = checkReadyState;
        function checkReadyState() {
          if (reader.readyState === 4) {
            //check to see whether request for the file failed or succeeded
            if ((reader.status == 200) || (reader.status === 0)) {
              //page exists - redirect to the clicked url
              document.location.href = url;
              // or 
              // window.open(url)
            } else {
              //if the url does not exist
              alert("No use going there!");
            }
          }
        }
        reader.send(null);
    
        return false;
    }
    
    //intercept link clicks
    document.onclick = iClicked;
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    正如@epascarello所说,你不能使用 return 在异步回调中。您需要稍后打开链接。

    此外,请记住,您不能在新选项卡中打开链接。 You'll have to open it in a new window or the same window. There's just no way around that.

    如果您仍然想这样做,以下是方法:

    if (!link) return true;
    
    var url = link.href;
    var ajaxurl = link.getAttribute('href');
    var needToCheck = url.indexOf('speed') != -1;
    //check if the url has the string 'speed' in it
    if (!needToCheck) return true;
    
    var reader = new XMLHttpRequest();
    //asynchronous is true
    reader.open('get', ajaxurl, true);
    //check each time the ready state changes
    //to see if the object is ready
    reader.onreadystatechange = checkReadyState;
    
    function checkReadyState() {
      if (reader.readyState === 4) {
        //check to see whether request for the file failed or succeeded
        if ((reader.status == 200) || (reader.status === 0)) {
          //page exists - redirect to the clicked url
          document.location.href = url;
          // or 
          // window.open(url)
        } else {
          //if the url does not exist
          alert("No use going there!");
        }
      }
    }
    
    return false;