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

前端 未结 10 1886
面向向阳花
面向向阳花 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

    javascript: in JS code (like in an onclick attribute) is just a label for use with continue/goto label statements that may or may not be supported by the browser (probably not anywhere). It could be zipzambam: instead. Even if the label can't be used, browsers still accept it so it doesn't cause an error.

    This means that if someone's throwing a useless label in an onclick attribute, they probably don't know what they're doing and are just copying and pasting or doing it out of habit from doing the below.

    javascript: in the href attribute signifies a Javascript URI.

    Example:

    javascript:(function()%7Balert(%22test%22)%3B%7D)()%3B
    
    0 讨论(0)
  • 2020-12-10 02:44

    The origins of javascript: in an event handler is actually just an IE specific thing so that you can specify the language in addition to the handler. This is because vbscript is also a supported client side scripting language in IE. Here's an example of "vbscript:".

    In other browsers (as has been said by Shadow2531) javascript: is just a label and is basically ignored.

    href="javascript:..." can be used in links to execute javascript code as DannySmurf points out.

    0 讨论(0)
  • 2020-12-10 02:46

    Probably nothing in your example. My understanding is that javascript: is for anchor tags (in place of an actual href). You'd use it so that your script can execute when the user clicks the link, but without initiating a navigation back to the page (which a blank href coupled with an onclick will do).

    For example:

    <a href="javascript:someFunction();">Blah</a>
    

    Rather than:

    <a href="" onclick="someFunction();">Blah</a>
    
    0 讨论(0)
  • 2020-12-10 02:46

    I don't know if the javascript: prefix means anything within the onevent attributes but I know they are annoying in anchor tags when trying to open the link in a new tab. The href should be used as a fall back and never to attach javascript to links.

    0 讨论(0)
提交回复
热议问题