Can javascript write to an html file?

element.InnerHtml= allmycontent: will re-write everything inside the element. You must use a variable let allmycontent="this is what I want to print inside this element"to store the whole content you want inside this element. If not each time you will call this function, the content of your element will be erased and replace with the last call you made of it.

document.write(): can be use several times, each time the content will be printed where the call is made on the page.

But be aware: document.write() method is only useful for inserting content at page creation . So use it for not repeating when having a lot of data to write like a catalogue of products or images.

Here a simple example: all your li for your aside menu are stored in an array "tab", you can create a js script with the script tag directly into the html page and then use the write method in an iterative function where you want to insert those li on the page.

`

This works because Js is interpreted in a linear way during the loading. So make sure your script is at the right place in your html page.You can also use an external Js file, but then again you must declare it at the place where you want it to be interpreted.

For this reason, document.write cannot be called for writing something on your page as a result of a "user interaction" (like click or hover), because then the DOM has been created and write method will, like said above, write a new page (purge the actual execution stack and start a new stack and a new DOM tree). In this case use:

element.innerHTML=("Myfullcontent");

To learn more about document's methods: open your console: document; then open: __proto__: HTMLDocumentPrototype

You'll see the function property "write" in the document object. You can also use document.writeln which add a new line at each statement. To learn more about a function/method, just write its name into the console with no parenthesis: document.write; The console will return a description instead of calling it.

Examples

Write some text directly to the HTML output:

document.write("Hello World!");

Try it Yourself »

Write some HTML elements directly to the HTML output:

document.write("

Hello World!

Have a nice day!

");

Try it Yourself »

Using document.write() after a document is loaded, deletes all existing HTML:

// This should be avoided:
function myFunction() {
  document.write("Hello World!");
}

Try it Yourself »

More examples below.


Definition and Usage

The write() method writes directly to an open (HTML) document stream.

Warning

The write() method deletes all existing HTML when used on a loaded document.

The write() method cannot be used in XHTML or XML.


Syntax

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

Parameters

Parameter Description
exp1,... Optional.
The output stream.
Multiple arguments are appended to the document in order of occurrence.

Return Value


More Examples

Write a date object directly to the HTML ouput:

document.write(Date());

Try it Yourself »

Open an output stream, add some HTML, then close the output stream:

document.open();
document.write("

Hello World

");
document.close();

Try it Yourself »

Open a new window and write some HTML into it:

const myWindow = window.open();
myWindow.document.write("

New Window

");
myWindow.document.write("

Hello World!

");

Try it Yourself »


The Difference Between write() and writeln()

The writeln( ) method is only useful when writing to text documents (type=".txt").

Example

document.write("Hello World!");
document.write("Have a nice day!");
document.write("
");
document.writeln("Hello World!");
document.writeln("Have a nice day!");

Try it Yourself »

Note

It makes no sense to use writeln() in HTML.

It is only useful when writing to text documents (type=".txt").

Newline characters are ignored in HTML.

If you want new lines in HTML, you must use paragraphs or
:

Examples

document.write("Hello World!");
document.write("
");
document.write("Have a nice day!");

Try it Yourself »

document.write("

Hello World!

");
document.write("

Have a nice day!

");

Try it Yourself »


Browser Support

document.write is supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

How do you add JavaScript to a HTML file?

Condition 1: Adding JS file using the file path In order to add your external JavaScript file into your HTML document, you have to provide your file path in the src attribute of the