简体   繁体   中英

Unable to print “hello world” message using javascript on my computer

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.

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.

下拉菜单的屏幕截图

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.

  1. There are separate parsers (software that takes code and determines what to do with it) for HTML and XML. I use the XML parser with HTML5 code so I gain the benefit of the latest technology with the much stricter XML parser. I've seen someone waste three days trying to figure out why Safari wouldn't properly style while all other browsers worked fine; he discovered he was missing a quote on an attribute. Using the XML parser the page immediately breaks and you get an error message.
  2. The XML parser works best in Gecko browsers such as Waterfox and Firefox. Other browsers render up to an error in the code.
  3. Stricter code might seem more difficult though it's been a massive boon for me. All the stuff I had to figure out has long been well documented presuming you know how to ask the questions you'll naturally encounter.
  4. Even if you use the HTML parser you should never use document.write or innerHTML as they're proprietary and really buggy. Those who disagree do not test browsers properly.
  5. Never put script elements in the body element, it leads to weak code that breaks easy.
  6. Use the 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.
  7. Spend time familiarizing yourself with JavaScript (and HTML/CSS) functions and methods to get an idea of how much is available to work with.
  8. Avoid frameworks and libraries and use normal pure JavaScript. You will save yourself vast amounts of frustration once you have the experience.
  9. Use Notepadd++ to edit files, Notepad in Windows adds a BOM (Byte Order Mark) that causes havoc with XML parsers and other aspects of code. Plus you can theme and get nifty things like bracket highlighting.

example.xhtml

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM