This also means that specifying the window keyword is optional:. For debugging purposes, you can call the console. The only exception is that you can call the window. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.
Tutorials References Exercises Menu. Buy Courses. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and jqvascript are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
W3Schools is Powered by W3.
Join Stack Overflow to learn, share knowledge, and build your career. Connect and share knowledge within a single location that is structured and easy to search. It seems that experienced web developers frown upon using document. I wouldn't say using document. In some cases you just need to have document. Look at how Google analytics gets injected into most websites. It's executed after the page has finished loading and does nothing more than write out a string of HTML.
The way you'd go about doing this would be to programmatically create your nodes and then attach them to an existing place in the DOM. For [purposes of a contrived] example, assuming that I've got a div element maintaining an ID attribute of "header," then I could introduce some dynamic text by doing this:.
Perhaps a good idea is to use jQuery in this case. It provides handy functionality and you can do things like this:. DOM methods are highly preferable to strings for setting attributes and content. But this isn't really the whole story. What is slow is inserting a lot of child nodes:. This translates to a load of work for the DOM finding the right place in its nodelist to insert the element, moving the other child nodes up, inserting the new node, updating the pointers, and so on.
Setting an existing attribute's value, or a text node's data, on the other hand, is very fast; you just change a string pointer and that's it.
This is going to be much faster than serialising the parent with innerHTML, changing it, and parsing it back in and won't lose your unserialisable data like event handlers, JS references and form values.
There are techniques to do DOM manipulations without so much slow childNodes walking. In particular, be aware of the possibilities of cloneNode , and using DocumentFragment. But sometimes innerHTML really is quicker. You can still get the best of both worlds by using innerHTML to write your basic structure with placeholders for attribute values and text content, which you then fill in afterwards using DOM.
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. But be aware: document. 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. In this case use:. You'll see the function property "write" in the document object. You can also use document. If you use document. From other hand other methods can't be used before DOM has been created page loaded , because they work directly with DOM. In most cases node. Most of the time it also make code more readable and smaller. The document. You can only use it before the page has finished loading. You can't use it to update the contents of a loaded page.
What you probably want is innerHTML. I think you should use, instead of document. Safe and almost cross browser. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 11 years, 6 months ago. Active 7 months ago. Viewed k times.
Why is this? Caitlin Webb 3 3 3 bronze badges. Gary Willoughby Gary Willoughby I didn't add this as an actual answer, but I came close. This is a case where the jQuery meme actually makes sense. You should totally drop that and use jQuery!
Tim: It is no more "correct" or "incorrect" than any of the methods given, but it is a perfectly valid approach. There is no one answer that is "correct" in all situations. Your given "disadvantage" is merely a characteristic: I may or may not to spend my time analyzing optimal strategies for HTML production when the potential benefits are marginal.
Depending on your choice, transparently managing that complexity could be an advantage or a disadvantage. One may decide to use jQuery until and unless there is a need for a more optimal strategy.
Adam - Well argued. It's certainly not a job worth using jQuery for on its own. Add a comment. Active Oldest Votes. Migisha 4 4 silver badges 10 10 bronze badges.
Ricky Ricky 5, 2 2 gold badges 29 29 silver badges 30 30 bronze badges. It's probably not what you want to do, but still you can. A nice full answer, thankyou. I understand now that document. Could you provide proof that innerHTML is always faster? I would doubt it's always the case. If you add nodes one at a time to an existing node in the DOM then I expect innerHTML is faster, since the browser potentially has to manipulate and re-render parts of the document at every stage, but if instead you create a new node tree and add its root node to the DOM at the end, I would expect that to be as fast or faster than using innerHTML.
Show 7 more comments. Tom Tom It's actually executed when you call it. If you call it after the page has loaded it will overwrite the content.
Stick to HTML and document. Nyla Pareska Nyla Pareska 1, 6 6 gold badges 22 22 silver badges 36 36 bronze badges. Because people are talking about the more elegant, faster and cross-browser way of doing something, and having to load jQuery to achieve it, is not an answer. DOM methods, as outlined by Tom. RoyalKnight RoyalKnight 5 5 bronze badges.
This doesn't apply if you're doing something "clever" like emulating a widget system. Rob Rob Mike Daniels Mike Daniels 8, 1 1 gold badge 28 28 silver badges 44 44 bronze badges. Luca Luca 1, 10 10 silver badges 4 4 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.