Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are confused. Unobtrusive JavaScript is not just about defining event handlers in a program. It's a set of rules for writing JavaScript such that the script doesn't affect the functionality of other JavaScript on the same page. JavaScript is a dynamic language. Anyone can make changes to anything. Thus if two separate scripts on the same page both define a global variable <code>add</code> as follows, the last one to define it will <em>win</em> and affect the functionality of the first script.</p> <pre><code>// script 1 var add = function (a, b) { return a + b; }; // script 2 add = 5; //script 1 again add(2, 3); // error - add is a number, not a function </code></pre> <p>Now, to answer your question directly:</p> <ol> <li>The extra overhead to find an element in JavaScript and attach an event listener to it is not a lot. You can use the new DOM method <code>document.querySelector</code> to find an element quickly and attach an event listener to it (it takes less than 1 ms to find the element).</li> <li>If you want to attach your event listeners quickly, don't do it when your document content loads. Attach your event listeners at the end of the body section or directly after the part of your HTML code to which you wish to attach the event listener.</li> <li>I don't see how altering the markup could affect the JavaScript in any manner. If you try to attach an event listener to an element that doesn't exist in JavaScript, it will silently fail or throw an exception. Either way, it really won't affect the functionality of the rest of the page. In addition, a HTML designer really doesn't need to know about the events attached any element. HTML is only supposed to be used for semantic markup; CSS is used for styling; and JavaScript is used for behavior. Don't mix up the three.</li> <li>God has given us free will. Use it. JavaScript supports conditional execution. There are <code>if</code> statements. See if <code>homePageDiv</code> exists and only then attach an event listener to it.</li> </ol> <p>Try:</p> <pre><code>$(document).ready(function () { $("ul.li").bind("click",doSomething()); if (document.querySelector(".homePageDiv")) { $(".homePageDiv").bind("click",doSomethingElse()); } else { $(".searchPageDiv").bind("click",doSomethingSearchy()); } }); </code></pre> <p>Your question had very little to do with unobtrusive JavaScript. It showed a lack of research and understanding. Thus, I'm down voting it. Sorry.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload