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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:24:00+00:00 2026-05-28T00:24:00+00:00

I am just learning JavaScript at the moment and I have been able to

  • 0

I am just learning JavaScript at the moment and I have been able to write an event handler for when a user clicks on the document that will output the mouse clicks coordinates.

However the problem I am having is that it will only do it once. I thought that if the event handler is waiting for a “click” and if it got one it would evoke the function getCords.

var x = document;
x.addEventListener("click", getCords, false);

function getCords(event){
    x.writeln(event.clientX, ",", event.clientY)
}
  • 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-28T00:24:00+00:00Added an answer on May 28, 2026 at 12:24 am

    On MDN:

    document.writeln is the same as document.write but adds a newline.

    So, let’s look at document.write:

    Writing to a document that has already loaded without calling
    document.open() will automatically perform a document.open call. Once
    you have finished writing, it is recommended to call document.close(),
    to tell the browser to finish loading the page. The text you write is
    parsed into the document’s structure model. In the example above, the
    h1 element becomes a node in the document.

    What does this mean?

    It means that your code really looks like this:

    var x = document;
    x.addEventListener("click", getCords, false);
    
    function getCords(event){
        x.open(); // <-------- new
        x.writeln(event.clientX, ",", event.clientY)
    }
    

    The first fix to make (which will also stop the page appearing as “loading” forever) is to add the document.close() call as advised:

    var x = document;
    x.addEventListener("click", getCords, false);
    
    function getCords(event){
        x.open(); // <-------- new
        x.writeln(event.clientX, ",", event.clientY)
        x.close(); // <-------- new
    }
    

    Now it’s clearer what’s going on — we’re creating a new document stream here. Your existing document contents are overwritten; the event handler ceases to exist.

    In fact, the DOM standard says this explicitly about open:

    Open a document stream for writing. If a document exists in the target, this method clears it.

    And your new document doesn’t contain your script, so even re-assigning the event handler inside the callback won’t work.

    Best not to use document.write (and document.writeln) at all; assign the text to be the contents of some div or span node instead.

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

Sidebar

Related Questions

I'm just starting out learning javascript, and tried to write a little script that
I have just started learning Jquery and am new to writing javascript (I am
I have just started writing my own JavaScript Framework (just for the learning experience),
I have just started learning Javascript. I want Hello World! to be written to
Hey, I've just started learning JavaScript and I'm making a little script that generates
I have just started learning Javascript and I am absolutely overwhelmed with the number
I m just learning regular expression matching using javascript, and have a doubt regarding
I am learning Javascript, I have been using PHP for about 10 years so
I am just learning how to use classes in my projects. I have been
I'm just now learning about file tasks in Rake. I have 100s of javascript

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.