What's the purpose (if any) of “[removed]” in event handler tags?

前端 未结 10 1940
面向向阳花
面向向阳花 2020-12-10 02:26

I\'ve been making a concerted effort to improve my javascript skills lately by reading as much javascript code as I can. In doing this I\'ve sometimes seen the javascr

10条回答
  •  忘掉有多难
    2020-12-10 02:43

    It should not be used in event handlers (though most browsers work defensively, and will not punish you). I would also argue that it should not be used in the href attribute of an anchor. If a browser supports javascript, it will use the properly defined event handler. If a browser does not, a javascript: link will appear broken. IMO, it is better to point them to a page explaining that they need to enable javascript to use that functionality, or better yet a non-javascript required version of the functionality. So, something like:

    Ajax me
    

    Edit: Thought of a good reason to use javascript:. Bookmarklets. For instance, this one sends you to google reader to view the rss feeds for a page:

    var b=document.body;
    if(b&&!document.xmlVersion){
      void(z=document.createElement('script'));
      void(z.src='http://www.google.com/reader/ui/subscribe-bookmarklet.js');
      void(b.appendChild(z));
    }else{
      location='http://www.google.com/reader/view/feed/'+encodeURIComponent(location.href)
    }
    

    To have a user easily add this Bookmarklet, you would format it like so:

    Drag this to your bookmarks, or right click and bookmark it!
    

提交回复
热议问题