Reputation: 33
I cannot print a helloworld message using Javascript in my HTML document. Let me explain my problem from beginning to end one-by-one.
I'm using the latest version of Microsoft Edge as my default web browser.
Using a simple notepad, I just created a simple HTML document named chandan.htm
and saved it to my hard disk.
I also included the script tag in the body of the HTML document in an intent to print the "hello world" message in java script code.
I share the simple HTML code, in which a simple Javascript code is embedded to print the "hello world" message:
<!DOCTYPE html>
<html>
<head>
<title> hello chandan how are you and what are you doing</title>
</head>
<body>
<script>
document.write("hello world ! ");
</script>
<h1>politics</h1>
</body>
</html>
When I run the HTML document in my Microsoft Edge browser, I cannot see the "hello world" message, that I have specified to print on the HTML document inside the script tag. I also gave a try by running this HTML document in Google Chrome and Internet Explorer hoping that it would print the "hello world" message once on the browser, but this thing did not happen at all. Instead, I could see only the HTML code, such as the text of the title, and the text of the heading tag of the HTML document; not "hello world" message from Javascript.
Before the date of May 30, 2020, everything was fine. The instructions of the Javascript, which were specified within the script tag of HTML document, were executed properly. I was able to print the "hello world" message using the Javascript command document.write("...")
. I could also see the "hello world" message directly on the screen. but after the date 30th May, 2020, I could not execute any Javascript code in any of my browser. 5-6 days after, when I tried to print the same "hello world" message again on my browser, the code was not executed and I cannot see the "hello world" message on my screen. could you resolve this problem?
I also tried to debug this problem at my level. What I double-clicked on the "chandan.htm" document, which opened the document in Edge, which is my default web browser. when I did not see the "hello world" message, I simply pressed "CONTROL-Shift-i" to open the console window. On the console prompt window, when I typed document.write("hello world")
and pressed "enter", then the console prints "undefined". That's it. so please help me to solve this problem soon.
Upvotes: 3
Views: 1128
Reputation: 13729
The problem is most people don't have the basics covered. The full working example is posted below though I'm going to make some clarifications first.
document.write
or innerHTML
as they're proprietary and really buggy. Those who disagree do not test browsers properly.script
elements in the body
element, it leads to weak code that breaks easy.defer="true"
attribute/value on script
elements in the head as this will require the HTML/XML to finish loading instead of executing a script before some of the expected HTML appears.<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example</title>
<script type="application/javascript">
//<![CDATA[
window.onload = function(event)
{
if (confirm('Would you like to add some text to the paragraph?'))
{
document.getElementById('example1').textContent = 'Hello World!';
}
}
//]]>
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="example1"></p>
</body>
</html>
Upvotes: 1
Reputation: 35512
When you save a file in notepad, .txt
is automatically appended to it and you can't see the extension due to file explorer's default settings. At the bottom of the save dialog, there should be a dropdown with the label "Save as type", and you want to click it and select "All files (*.*)", so .txt
isn't automatically appended to it. Then, when you open it, it should appear as html and not as text.
Upvotes: 3