Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:45:51+00:00 2026-05-24T05:45:51+00:00

I find it convenient to set a variable with the same name as an

  • 0

I find it convenient to set a variable with the same name as an element’s id, for example:

randomDiv = document.getElementById("randomDiv");
  randomDiv.onclick = function(){ /* Whatever; */ }
  randomDiv.property = "value";

This works in Chrome and Firefox, but not IE8; giving the error Object doesn’t support this property or method.

Is creating a variable with a name that matches an element ID wrong (or bad practice) or is this another instance of Internet Explorer acting up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:45:52+00:00Added an answer on May 24, 2026 at 5:45 am

    Making global variables automatically is considered bad practice because it can be difficult to tell, looking at some code, whether it is on purpose or you forgot to declare a variable somewhere. Automatic creation of global variables like this doesn’t work in ES5 strict mode and could be phased out phased out in future versions of ECMAScript.

    In the browser JavaScript’s global scope is actually window. When you refer to document you get window.document. Best practice for creating a global variable in a browser is to add it to window (global in Node.js). Here’s an example from jQuery:

    window.jQuery = window.$ = jQuery;
    

    Some properties on window (hence some global variables) are read-only, you can’t overwrite them. window.document is one (tested in Chrome, this is all browser-specific and could change):

    window.document; // → Document
    window.document = 'foo'; // → "foo" // It worked!
    window.document; // → Document // Hmm, no it didn’t
    

    It turns out that most browsers create properties on window (hence global variables) for each id in the document. Many browsers don’t make them read-only, you can overwrite them with your own, but Internet Explorer does.

    This is another reason global variables in JavaScript can be dangerous — one of your ids could match a read-only window property (today or in some future browser).

    At the top level (not inside a function), var declares global variables. Stating var document = 'foo' at the top level won’t throw an error but document will still be the Document, not "foo".

    As an aside: new-ish browsers (which support ES5) let you create your own read-only globals with Object.defineProperty:

    Object.defineProperty(window, 'foo', { value: 'bar', writable: false });
    foo = 'baz';
    foo; // → "bar"
    

    I’ve got three options for you.

    1. Keep using global variables for your elements but leave them alone if they already exist (creating them on window explicitly so the code is clear and cool with ES5):

      if ( ! window.randomDiv) {
          window.randomDiv = document.getElementById('randomDiv');
      }
      
    2. Create an object, on window, to use as your app’s own namespace which won’t interfere with other libraries or with the browser. This is common and considered pretty good practice, especially if it needs to be accessed across JavaScript files:

      // Early in your code…
      window.Fantabulum = {};
      // Later on…
      Fantabulum.randomDiv = document.getElementById("randomDiv");
      
    3. Avoid making globals. Make sure that your application’s code is inside a function (it should be already so your other variables aren’t global and don’t have the same limitations!), and declare variables for your elements:

      (function(){
          var randomDiv = document.getElementById("randomDiv");
      })();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find it very convenient to set arbitary attributes like: <a stackoverflowId=123>...</a> And in
When creating a UserControl in WPF, I find it convenient to give it some
I parse data using JSON in javascript, I find this is very convenient. But
I'm a beginner in Objective-C and I'm trying to find the most convenient way
I'm looking for the convenient signwizard in signtool.exe but cannot find it anymore in
I have several procedures that use the FileSystemObject. I find it's quite convenient. Question:
I find it convenient to store list adapters as class members in my activities.
I'm attempting to embed the very convenient Google Translate translate element into a webpage,
find whether there is a loop in a linked list. Do you have other
find . -type f -print prints out ./file1 ./file2 ./file3 Any way to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.