0

Javascript, running on strict mode, does nothing if you use an id's value as variable, even without declaring it. Here is an example (see how linkhome and linkshop are not declared):`

'use strict';
var gameWorld = {
  contentoutput: document.getElementById("contentoutput"),
  linkupgrade: document.getElementById("linkupgrade"),
  linkachievement: document.getElementById("linkachievement"),


  addEventHandlers: function() {
    linkhome.addEventListener("click", function() {
      loadContent("contenthome")
    });
    linkshop.addEventListener("click", function() {
      loadContent("contentshop")
    });
    linkupgrade.addEventListener("click", function() {
      loadContent("contentupgrade")
    });
    linkachievement.addEventListener("click", function() {
      loadContent("contentachievement")
    });
    linkajaxhome.addEventListener("click", loadHtml);
  }
}

function loadContent(contentid) {
  var x = document.getElementById(contentid).innerHTML;
  contentoutput.innerHTML = x;
}


function init() {
  gameWorld.addEventHandlers();
}

document.addEventListener("DOMContentLoaded", init);
.hidden {
  display: none
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
</head>

<body>
  <nav>
    <ul>
      <li><a href="#" id="linkhome">Home</a>
      </li>
      <li><a href="#" id="linkshop">Shop</a>
      </li>
      <li><a href="#" id="linkupgrade">Upgrade</a>
      </li>
      <li><a href="#" id="linkachievement">Achievement</a>
      </li>
    </ul>
  </nav>

  <section id="contentoutput">
  </section>

  <div class="hidden">
    <div id="contenthome">
      <h1>Home Page</h1>
      <p>This is the text of the home page</p>
    </div>
    <div id="contentshop">
      <h1>Shop Page</h1>
      <p>This is the text of the shop page</p>
    </div>
    <div id="contentupgrade">
      <h1>Upgrade Page</h1>
      <p>This is the text of the upgrade page</p>
    </div>
    <div id="contentachievement">
      <h1>Achievement Page</h1>
      <p>This is the text of the achievement page</p>
    </div>
  </div>

</body>

</html>

` I have two questions :

  • Is there any way to stop this behavior in javascript?
  • In this specific code, how can I refer to the variables initialized in the gameWorld object in the addEventHandlers function? (gameWorld.linkupgrade or this.linkupgrade don't work)
bpbutti
  • 381
  • 1
  • 8
  • Usually `id`s work as variables in quirks mode. Add ` ` to your html file running that javascript and it should be fine. – Sebastian Simon Apr 02 '15 at 03:28
  • 1
    Related: http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables , http://www.w3.org/html/wg/drafts/html/master/browsers.html#named-access-on-the-window-object – jkd Apr 02 '15 at 03:31
  • @Xufox It looks like there was discussion about moving this to quirksmode, but the conversation here: https://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c25 seems to indicate it wasn't the case. Specifically: "Let's take the note out of the spec then. :0)" when asked about the memo "It is possible that this will change. Browser vendors are considering limiting this behaviour to quirks mode." – Jack Apr 02 '15 at 03:35
  • Notice that you have not initialised any *variables* - those are *properties* of the `gameWorld` object. Therefore, `gameWorld.linkupgrade` will work (supposed [it does actually have a value](http://stackoverflow.com/q/14028959/1048572)), while indeed the `contentoutput` variable should not exist. – Bergi Apr 02 '15 at 03:45
  • @Bergi Thanks, you are right. The problem with this part was that i had my scripts loading between the head tags. I moved it to the end of the html and `gameWorld.linkupgrade` worked. – bpbutti Apr 02 '15 at 03:56
  • @Bergi I think the stack question, posted by jakekimds, as related to this one, answers this question better. The one you set as the duplicated also does, but the main question was about DOM elements ids becoming global variables. Thanks you all for helping. – bpbutti Apr 02 '15 at 04:24

0 Answers0