Important

Okay
  Public Ticket #1435143
Inline Javascript
Closed

Comments

  • Jimmy Heibert started the conversation

    In some of the files (I haven't looked through a lot of them, yet) there are inline Javascript added in <script> tags. One example is the file app/code/Ves/Megamenu/view/frontend/templates/topmenu.phtml that is the default template file used for adding menus. Even if we disregard the fact that Magento itself discourage the use of inline Javascript, there are a lot of disadvantages with using inline Javascript. One in particular is the fact that there is a high risk of functions breaking if the file is imported multiple times. In the above file, one example of this is the function "menuToggle", found on lines 160 through 172, and the click binding on line 173. When this file gets imported multiple times, each file binds this function to the same click event, causing the function to run multiple times for each click, and therefore breaking the functionality.

    I have two main approaches to fix this issue. One is to override the entire template file, but I would prefer to leave it unchanged, to be able to not block future updates. The other is to override each binding in a separate .js file, but one big drawback is that there is a delay between the loading of the inline scripts and my .js file (that runs after domReady), causing the site behavior to change during loading.

    This way of adding inline Javascript is bad practice, and I would strongly encourage you to move it into separate .js files in future updates. Then the delay would be eliminated and the edit of code would be easier.

  •  1,319
    Land of replied

    Hello Jimmy,

    Have a nice day!

    Thanks you for your comment!

    We will notice to update it on next version.