onclick=“” vs event handler

前端 未结 8 2193
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:20

If I want a function to be executed, I prefer doing inline js:

Click me

becaus

相关标签:
8条回答
  • 2020-11-22 00:42

    I see some saying there needs to be a separation of concerns regarding Presentation and Business Logic.

    OP in his example does indeed show this separation! There is no logic in the inline event handler, but merely a function reference/call that will get executed on the "click" event... the logic itself can be separately maintained elsewhere.

    I personally prefer this approach due to logical flow discoverability. If I have never seen an application before... the first place I'm going to begin my code traversal is in the DOM, and right there it will be clear which event handlers are in play and which functions are providing the logic to the handlers. With using, say a "JQuery.On" eventing approach, by just glancing through the html you would have no idea which handlers are actually wired up and providing functionality.

    Inline event handlers that simply provide pointers to functions is simply wiring up the events and not leaking logic into presentation.

    0 讨论(0)
  • 2020-11-22 00:48

    There are a lot of reasons to avoid inline JavaScript and one of the perhaps most important is code maintainability.

    A quick example (I am using jQuery simply for demonstration purposes).

    <p class="element" onclick="doSomething();">Click me</p>
    <p class="element" onclick="doSomething();">Click me</p>
    <p class="element" onclick="doSomething();">Click me</p>
    <p class="element" onclick="doSomething();">Click me</p>
    <p class="element" onclick="doSomething();">Click me</p>
    <p class="element" onclick="doSomething();">Click me</p>
    

    What if suddenly you get a request to change all your paragraphs to execute another function? In your example you would have to change everything manually in your HTML code. However if you choose to separate HTML from JavaScript you could simply do it like this.

    <p class="element">Click me</p>
    <p class="element">Click me</p>
    <p class="element">Click me</p>
    <p class="element">Click me</p>
    <p class="element">Click me</p>
    <p class="element">Click me</p>
    
    $('.element').bind('click', doSomethingElse);
    

    The HTML code is also cleaner which allows the designers to focus exclusively on design without fear that they might actually break something while working on a project which also involves other people.

    EDIT: Providing example for my comment bellow.

    Project = {
        // All the variables/constants/objects that need to be globally accessible inside the Project object.
    
        init : function(){
            // Main entry point...
            this.MainMenu.init();
    
            // Rest of the code which should execute the moment Project is initiated.
        }
    }
    
    Project.MainMenu = {
        // All the variables/constants/objects that need to be accessible only to MainMenu.
    
        init : function(){ // Is run immediatelly by Project.init()
            // Event handlers relevant to the main menu are bound here
    
            // Rest of the initialization code
        }
    }
    
    Project.SlideShow = {
        // All the variables/constants/objects that need to be accessible only to SlideShow.
    
        init : function(){ // Is run only on pages that really require it.
            // Event handlers for the slideshow.
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:48

    Apart from the coding standards perspective

    Using Attributes:

    <p id="element" onclick="doSomething();">Click me</p>

    if you add the same attribute again (see below) -the latest one is considered.

    <p id="element" onclick="doSomethingMore();">Click me</p>

    Using Event Handler:

    document.getElementById('element').onclick = doSomething;

    and lets say you added the below line

    document.getElementById('element').onclick = doSomethingMore; Both the handlers are invoked.

    0 讨论(0)
  • 2020-11-22 00:48

    I'm not aware of and advantage of using inline handler vs attach the handler later. In my experience I prefer to use the inline method when I have to deal with dynamic elements, for example if I want to add an handler to each image and the number of images changes according to other data (ex images in db). In this case using inline allow me to add an handler to each image, otherwise i have to recalculate the number of images in the javascript file to attach and handler to each image.

    Of course, when you can use libraries like jQuery where you can easily get the list of elements inline aren't useful

    0 讨论(0)
  • 2020-11-22 00:49

    You could say the same about CSS and including inline styles. It would be easy to not have to wade through a css file to find the current class/id, or parent/child elements your working with.

    Is it really better to bind a click to say 10 different items inline? Or just one event handler targeting a class? Even if you don't need 10 click handlers its always a better idea to set your code up for maintainability, and upgrades down the road.

    0 讨论(0)
  • 2020-11-22 00:52

    Basically it has to do with the whole keep everything separate I believe. So keep HTML/CSS/JS all separate. It makes your HTML tidier and, I think, easier to navigate without.

    Then when/if you need to make large changes, you have ample space with having to shift the inline JS to an external file anyway OR if you want to apply the same function to more than one button, then it's less code. And less code is a happier place

    If you have your JS files properly, and thoroughly documented then navigating them by an outside person is made eaiser

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