Javascript有没有办法检测已请求的页面

在点击a-tag的时刻,浏览器开始请求URL.有没有办法检测该请求以及URL请求是什么? 最佳答案

At the very moment an a-tag has been clicked and the browser commences
to request the URL

如果您确定仅通过单击链接请求页面(URL),则可以将事件侦听器添加到页面的锚标记

在纯JS中

var allAnchors = document.getElementsByTagName("a");
for ( var counter = 0; counter < allAnchors.length; counter++)
{
    allAnchors[counter].addEventListener( "click", function(e){
      e.preventDefault();
      var src = this.getAttribute( "href" );
      alert( "This link was clicked " + src )
      location.href = src;
    }, false );
}

在jQuery中

$( "a" ).click( function(){
      e.preventDefault();
      alert( "This link was clicked " + $( this ).attr( "href" ) );
      location.href = $( this ).attr( "href" );
} );
点赞