href javascript limitations

href javascript limitations

本文关键字:limitations javascript href      更新时间:2023-09-26

大多数人出于风格原因不会在href中使用javascript。事实上,你在href中所能做的事情似乎受到了严重的限制。不幸的是,开发者控制台无法提供帮助,所以我希望你可以。

     <a href="javascript:alert('high five');
     var request = new XMLHttpRequest();
     request.onreadystatechange = function () {
     var DONE = this.DONE || 4;
     if (this.readyState === DONE){
     alert(this.readyState);
     }
     };
     request.open('GET', 'http://www.mototale.com', true);
     request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); 
     request.send(null);
     ">mosh mosh</a>

但这不是:

    <a href="javascript:alert('high five');
    var foo=document.getElementById('ko');
    function doMove() {foo.style.left =
      parseInt(foo.style.left)+1+'px';setTimeout(doMove,20);};
    function init(){foo.style.left = '0px';doMove();};
    init();">move</a>   
    <p id="ko">ok</p>

和一些变体

请不要建议使用onclick或unobtrusive,那不在这个问题的范围之内。在hrefjavascript:有哪些限制?

调用函数后需要()。把

init;">move</a> 

:

init();">move</a> 

在第二个版本,它应该工作

在第二个例子中您从不调用init

<a href="javascript:...init();">move</a>