How does inline Javascript (in HTML) work?

前端 未结 6 693
醉酒成梦
醉酒成梦 2020-11-28 19:21

I know this is bad practice. Don\'t write code like this if at all possible.

Of course, we\'ll always find ourselves in situations where a clever snippet of inline

相关标签:
6条回答
  • 2020-11-28 19:46

    There seems to be a lot of bad practice being thrown around Event Handler Attributes. Bad practice is not knowing and using available features where it is most appropriate. The Event Attributes are fully W3C Documented standards and there is nothing bad practice about them. It's no different than placing inline styles, which is also W3C Documented and can be useful in times. Whether you place it wrapped in script tags or not, it's gonna be interpreted the same way.

    https://www.w3.org/TR/html5/webappapis.html#event-handler-idl-attributes

    0 讨论(0)
  • 2020-11-28 19:49

    The best way to answer your question is to see it in action.

    <a id="test" onclick="alert('test')"> test </a> ​
    

    In the js

    var test = document.getElementById('test');
    console.log( test.onclick ); 
    

    As you see in the console, if you're using chrome it prints an anonymous function with the event object passed in, although it's a little different in IE.

    function onclick(event) {
       alert('test')
    }
    

    I agree with some of your points about inline event handlers. Yes they are easy to write, but i don't agree with your point about having to change code in multiple places, if you structure your code well, you shouldn't need to do this.

    0 讨论(0)
  • 2020-11-28 19:50

    It looks suspicious because there is no apparent function that is being returned from!

    It is an anonymous function that has been attached to the click event of the object.

    why are you doing this, Steve?

    Why on earth are you doi.....Ah nevermind, as you've mentioned, it really is widely adopted bad practice :)

    0 讨论(0)
  • 2020-11-28 19:54

    You've got it nearly correct, but you haven't accounted for the this value supplied to the inline code.

    <a href="#" onclick="alert(this)">Click Me</a>
    

    is actually closer to:

    <a href="#" id="click_me">Click Me</a>
    <script type="text/javascript">
    document.getElementById('click_me').addEventListener("click", function(event) {
        (function(event) {
            alert(this);
        }).call(document.getElementById('click_me'), event);
    });
    </script>
    

    Inline event handlers set this equal to the target of the event. You can also use anonymous function in inline script

    <a href="#" onclick="(function(){alert(this);})()">Click Me</a>
    
    0 讨论(0)
  • 2020-11-28 20:00

    Try this in the console:

    var div = document.createElement('div');
    
    div.setAttribute('onclick', 'alert(event)');
    
    div.onclick
    

    In Chrome, it shows this:

    function onclick(event) {
      alert(event)
    }
    

    ...and the non-standard name property of div.onclick is "onclick".

    So, whether or not this is anonymous depends your definition of "anonymous." Compare with something like var foo = new Function(), where foo.name is an empty string, and foo.toString() will produce something like

    function anonymous() {
    
    }
    
    0 讨论(0)
  • 2020-11-28 20:05

    What the browser does when you've got

    <a onclick="alert('Hi');" ... >
    

    is to set the actual value of "onclick" to something effectively like:

    new Function("event", "alert('Hi');");
    

    That is, it creates a function that expects an "event" parameter. (Well, IE doesn't; it's more like a plain simple anonymous function.)

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