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 7616107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:48:36+00:00 2026-05-31T02:48:36+00:00

I have the following javascript code: <script type=text/javascript> var x = 10; window.onload =

  • 0

I have the following javascript code:

<script type="text/javascript">
   var x = 10;
   window.onload = function() {
      document.write(this.x); // <-- yields correct output: 10.
      document.write(this.x); // <-- outputs "undefined"
      document.write(this.x); // <-- outputs "undefined"
   }
</script>

I am unable to understand why [this.x] results in undefined from the second time onwards. If I am correct, the function context (value of “this”), would refer to the global window object.

  • 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-31T02:48:37+00:00Added an answer on May 31, 2026 at 2:48 am

    When you use document.write() AFTER the document has loaded, it opens a new document (clearing the previous one). It appears that in some browsers (IE, for example), the global variables are immediately wiped out, even from the script that is still running that did the document.write(). The best answer here is to not use document.write() after the page has loaded. Instead, use DOM manipulation to change the existing document however you want it changed rather than creating a new document. You can see that things work fine (based on your more recent code example) if you use DOM manipulation (manipulating innerHTML) rather than using document.write() in this jsFiddle.

    Since clearing the current document is almost never what you want to do, if you can explain what you’re really trying to accomplish we can help better with a solution.

    If you want to modify the existing document after it has loaded, you need to use DOM manipulation functions such as .innerHTML (to change the HTML of a node) or DOM manipulation functions to add new nodes to the existing document, NOT document.write().

    You should generally not use this to refer to global variables. Global variables are available with no prefix (unless overriden locally) or available with the window prefix.

    So, either of these will work:

    <script type="text/javascript">
       var x = 10;
       window.onload = function() {
          console.log(x);
          console.log(x);
          console.log(x);
       }
    </script>
    
    <script type="text/javascript">
       var x = 10;
       window.onload = function() {
          console.log(window.x);
          console.log(window.x);
          console.log(window.x);
       }
    </script>
    

    As to your question about using this. The value of this is set one of several ways:

    1. When you call a method on an object such as pleasures.makeIceCream(), the value of this will be set to the pleasures object in the makeIceCream() method.
    2. When you use the call method on function objects like makeIceCream.call(pleasures), then the value of this will be set to the pleasures object in this invocation of the makeIceCream() method.
    3. When you use the apply method on function objects like makeIceCream.apply(pleasures), then the value of this will be set to the pleasures object in this invocation of the makeIceCream() method.

    You can read more about .call() and .apply() in these MDN references:

    apply: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply

    call: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call

    At all other times, the value of this should generally not be used as it has not been explicitly set. It is likely set to the global object (in a browser, this is the window object) , but it is not considered best practice to use it to access global variables, nor is there any reason to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =
So I have the following JavaScript code: <script type=text/javascript> function clearRadioButtons() { document.getElementById(radiobutton1).checked=; //etc
I have the following code: <script type=text/javascript> $(document).ready(function() { $(#Save).click(function() { $.post(url, { data:
I have the following code: <script type=text/javascript> function SubmitForm() { form1.submit(); } function ShowResponse()
I have the following code <div class=framepage> <header> <script type=text/javascript> function getBaseUrl() { return
I have the following code: JS <script type=text/javascript> $(function(){ $('#fgotpwfield').hide(); $('#login_submit').click(function() { $('#form_result').fadeOut('fast'); $('#myccrxlogin
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==
I have written the following in a JavaScript method : <html> ... <script type=text/javascript
Alright, to start with let's look at some code: <html> <body> <script type=text/javascript> function
I have the following JavaScript code to pop up a window in Internet Explorer.

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.