When is the '[removed]' prefix valid syntax?

后端 未结 3 1156
终归单人心
终归单人心 2020-12-15 08:33

I know that you can use a javascript: pseudo protocol for URLs in an tag. However, I\'ve noticed that Firefox and IE will both allow \'javascri

相关标签:
3条回答
  • 2020-12-15 09:09

    One thing to consider, our testers would always ding us if we did something like

    
    <a href='javascript:openwindowmethod("url");'> stuff </a>
    

    Rather than

    
    <a href='url' onclick='return openwindowmethod(this.href);'> stuff </a>
    

    The first method would only work if you click on it but not if you shift or alt clicked on it, or right clicked and went to open in a new window.

    The second method would support all of that, as well as the ability to function the way it intended if the user just plain clicked the link.

    0 讨论(0)
  • 2020-12-15 09:11

    You need the javascript: "protocol" when you want to put JavaScript in the href attribute of a link.

    <!-- does not work -->
    <a href="alert('some text');">link</a>
    
    <!-- does work -->
    <a href="javascript:alert('some text');">link</a>
    
    <!-- also works -->
    <a href="#" onclick="alert('some text');">link</a>
    

    As far as I know (and please, if I'm wrong, someone correct me) there is no difference in scope, but there is a very important difference about this.

    <!-- does not work -->
    <a href="alert(this.href);">link</a>
    
    <!-- alerts "undefined" -->
    <a href="javascript:alert(this.href);">link</a>
    
    <!-- works as expected, alerts "<url>#" -->
    <a href="#" onclick="alert(this.href);">link</a>
    
    0 讨论(0)
  • 2020-12-15 09:20

    Outside of the href attribute (where it is a protocol specifier), name: just creates a label (such as one might use with a continue or break).

    See: Do you ever need to specify javascript: in an onclick?

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