0

I have a written a simple javascript function that will write the variable value that is outside of the function using document.write and call the function. However, the code doesn't work:

var name = 'guru';
function type () {
  document.write = name;
}

I then tried returning document.write. However, this also did not work.

var name = 'guru';
function type () {
  return document.write = name;
}
type();

In this code, I just return the variable and use document.write outside of the function. This works.

var name = 'guru';
function type () {
    return name;
}
document.write = type();

Why is this happening? Can someone explain this to me?

edit

The code here doesn't work in firebug, because the firebug outside HTML5 document doesn't recognize document.write as a function, see the error in the image. That is if you fire up firebug and run this doesn't work but if it is run inside a HTML DOM this is an error, this is a bug or this is how it works.

firebug error

7
  • 3
    I think you need document.write (somevariable) Commented May 11, 2015 at 7:24
  • Did you intend to call the document.write function? Because none of the three versions seem to do that. They redefine document.write instead. Commented May 11, 2015 at 7:24
  • as @HoboSapiens pointed out document.write is function Commented May 11, 2015 at 7:26
  • @HoboSapiens nope !! because firbug doesn't recognize document.write as a function, this is correct according to firebug . Wait I ll try it with in the HTML DOM. Commented May 11, 2015 at 7:27
  • do you mean "document.write(name)"? Commented May 11, 2015 at 7:28

6 Answers 6

3

You shouldn't use document.write, but if you absolutely must:

var writeSomething = function (message) {
    document.write(message);
}

If you're trying to debug, I recommend using console.log. It's supported by all modern browsers and is much more useful (it lets you inspect objects, arrays, etc.). The problem with document.write is that it destroys the current document to write the new information (see note at end).

Here's an example use of this function:

var writeSomething = function (message) {
    document.write(message);
}

document.getElementById("myButton").onclick = function () {
    var message = document.getElementById("myMessage").value;
    writeSomething(message);
}
<button id="myButton">click me</button>
<input id="myMessage" placeholder="type message here">

from MDN on why document.write is a no-no:

Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open which will clear the document.

tl;dr? avoid document.write like the plague.

Sign up to request clarification or add additional context in comments.

Comments

2

The document.write is a function. The syntax is as follows:

document.write(exp1,exp2,exp3,...)

The parameters are optional. Multiple arguments can be listed and they will be appended to the document in order of occurrence

Comments

2

document.write is a function, which you need to call like so:

var myHtml = "<html><head></head><body><div><span>Hello World!</span></div></body></html>";

document.write(myHtml);

Comments

2

When you do document.write = name;, you are overwriting the function.

You must use it like: document.write(message);

http://codepen.io/ces/pen/GJoxbX

Comments

2

According to document.write syntax you should pass it an argument that will be actual DOM string.

By doing

function type() {
return name;
}
document.write = type();

You you actually overwrite the original document.write function. So now if you call document.write('foo') you will get Uncaught TypeError: document.write is not a function. Proper way however of using document.write could be:

document.write("<h1>Foo</h1>");

Comments

2

A.

function type() {
var name = 'guru';  
 document.write (name);
}

B.

var name = 'guru';
  function type( text ) {
  document.write(text);

  }
  type( name );

You have do this like this.Look at it once,it helps you to under stand.

3 Comments

You should not return document.write(arguments); that'll only ever return undefined, since document.write is the equivalent of a void function.
Setting document.write equal to x, before returning x, is no different.
There should be no return value wii be there i think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.