imonaboat
imonaboat

Reputation: 29

href javascript limitations

Most people won't use javascript in href for style reasons. Fact is it appears that there are severe limitations to what you can do inside href. Unfortunately the developer console cannot help out, so I'm hoping you can. This does work:

     <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>

but this doesn't:

    <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>

and a number of variants.

Please don't advice to use onclick, or unobtrusive, that is outside the scope of this question. What limitations are there to javascript: in href ?

Upvotes: 0

Views: 88

Answers (2)

Mike Cluck
Mike Cluck

Reputation: 32511

You never call init in the second example.

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

Upvotes: 0

Barmar
Barmar

Reputation: 781761

To call a function you need () after it. Change

init;">move</a> 

to:

init();">move</a> 

in the second version and it should work.

Upvotes: 1

Related Questions