blog icon indicating copy to clipboard operation
blog copied to clipboard

出现undefined请求的案例

Open mrdulin opened this issue 7 years ago • 0 comments

最近查看nginx访问日志时,发现几个页面出现undefined请求。

查看代码,发现问题,原因是点击如下图中的ulpadding空白区域导致:

模拟代码:

<ul class='list'>
    <li class='item'>
      <img src="http://7xp9vw.com1.z0.glb.clouddn.com/Group%2020.png" alt="//github.com">
    </li>
    <li class='item'>
      <img src="http://7xp9vw.com1.z0.glb.clouddn.com/Group%2022.png" alt="//google.com">
    </li>
    <li class='item'>
      <img src="http://7xp9vw.com1.z0.glb.clouddn.com/Group%2040.png" alt="//music.163.com">
    </li>
  </ul>
window.onload = function () {
      const doc = document;
      const dom = {};
      dom.list = doc.querySelector('.list');
      dom.list.addEventListener('click', function (e) {
        const target = e.target;
        //与index.html在同一个域名的undefined请求,原因是如果点击事件发生在ul的padding区域,target是ul而不是img
        console.log('target:', target);
        const url = target.alt;
        window.location.href = url;
      }, false);

      //还有一种可能就是图片的src是undefined,也会发出undefined的请求
      const img = new Image();
      img.src = undefined;
    };

会出现如下的undefined请求:

mrdulin avatar Nov 30 '17 07:11 mrdulin