Simple Javascript test not being exectuted - javascript

I'm using Notepad++ to write a simple JavaScript program. I tried to run it with Firefox but the page was blank. I saved it as a HTML file but still nothing. Where am I going wrong?
Here is the code:
<!DOCTYPE html>
<html>
<head>
<title>Exercise 1 </title>
</head>
<body>
<script type = “text/javascript”>
var myName = "type your name!!";
document.write("Hello World");
</script>
</body>
</html>

These look like left quote/right quote characters:
<script type = “text/javascript”>
^ ^
They should be regular double-quote characters <script type="text/javascript">, or you can leave them out entirely, as all browsers default to using Javascript:
<script>
</script>
Additionally, it is not recommended to use document.write any longer. You should use some of the DOM manipulation methods available instead.

As pointed out you are using the wrong type of quotes. Note that you can download jshint for notepad++ which can be very useful as it catches these types of bugs before you run. At the time of writing this answer, you can get it from sourceforge here. It includes jslint and jshint.
JSHint is a community-driven tool to detect errors and potential
problems in JavaScript code and to enforce your team's coding
conventions. It is very flexible so you can easily adjust it to your
particular coding guidelines and the environment you expect your code
to execute in.

Related

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.
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.
The XML parser works best in Gecko browsers such as Waterfox and Firefox. Other browsers render up to an error in the code.
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.
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.
Never put script elements in the body element, it leads to weak code that breaks easy.
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.
Spend time familiarizing yourself with JavaScript (and HTML/CSS) functions and methods to get an idea of how much is available to work with.
Avoid frameworks and libraries and use normal pure JavaScript. You will save yourself vast amounts of frustration once you have the experience.
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>

jslint error - #1 Expected '(end)' and instead saw '<!'

I am typing in a v simple example into sublime text. I have the SublimeLinter and JSLint plugins installed, but am getting the following error:
#1 Expected '(end)' and instead saw '<!'.
The code is a simple HTML file, which may be my problem. I don't know.
The code is as follows:
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
If it is true that either linting plugin is causing this error what should I do?
The error is generated by JSLint. By default, it runs on files with extensions .js, .json, .html, .sass, and .less. To prevent the error without disabling or removing JSLint, try removing HTML files from this list.
To do this, open JSLint.sublime-settings. Around line 35 you should see the following property.
,"filename_filter": "(\\.js|\\.json|\\.html|\\.sass|\\.less|\\.html)$"
Erase both |\\.html entries so that you're left with this.
,"filename_filter": "(\\.js|\\.json|\\.sass|\\.less)$"
Save your file. You may need to restart Sublime Text. Also note that you can still run JSLint on an HTML file using the following shortcut.
Control + L

D3.js: "Uncaught SyntaxError: Unexpected token ILLEGAL"?

I've just downloaded D3.js from d3js.org (link to zip file), unzipped it, and referenced it in the following HTML page:
<html>
<head>
<title>D3 Sandbox</title>
<style>
</head>
<body>
<script src="/d3.v3.js"></script>
</body>
</html>
But when I load this page, my console (in Chrome) is giving me this error:
Uncaught SyntaxError: Unexpected token ILLEGAL: line 2
It doesn't like the pi and e symbols at the start of the file. Errrr... what can I do about this? I am serving the file with python's SimpleHTTPServer.
Update: yes I know I can just link to a CDN version, but I would prefer to serve the file locally.
Try specifying the UTF-8 charset on the HTML host document :
<meta http-equiv="content-type" content="text/html; charset=UTF8">
D3 contains UTF-8 symbols (like π) invalids in non-UTF8 documents.
That sounds like a problem with encoding. I recommend The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Despite the somewhat condescending title, it contains some very useful information. Specifically, it sounds like your server is serving the d3.v3.js file with the wrong encoding.
Add 'charset="utf-8"'
<script src="/d3.v3.js" charset="utf-8"></script>
I tried setting the charset in the doc and in the script tag itself, but Chrome doesn't seem to care. Not sure if i'm doing something wrong.
I did find success by running it through uglify with the --ascii-only option first.
UPDATE: Turns out my View->Encoding setting in Chrome was not on Auto-Detect, but some Western encoding. Not sure why, but changing that resolved the problem. Ridiculous that that setting would trump a charset property right on the script tag. Considering that users might be in the same situation and not be able to figure it out, i'll still be using uglify to ensure success.
Check if you have a plus sign between all of your string concatenations, if you do not this error will be generated.

HTML Opening-Comment is valid JavaScript?

An old idiom for getting very old browsers to ignore JavaScript blocks in HTML pages is to wrap the contents of the <script> element in HTML comments:
<script>
<!--
alert("Your browser supports JavaScript");
//-->
</script>
The rationale is that old JavaScriptless browsers will render as text the contents of the <script> element, so putting the JavaScript in an HTML comment makes the browser have nothing to render.
A modern browser, on the other hand, will see the <script> element and parse its contents as JavaScript. Consequently, the comments need to be valid JavaScript. The closing HTML comment (-->) is ignored by the JavaScript parser because it is preceded by a JavaScript line-comment (//).
My question is, how does the opening HTML comment (<!--) not cause the JavaScript parser to fail? I have heard from various people that the opening HTML comment is valid JavaScript. If it's true that the opening comment is evaluated as JavaScript, what does it do when it executes?
It seemed to be something exciting, an expression that might have a special meaning (<, ! and -- are all operators in Javascript), but without operands it does not make sense.
Turns out that <!-- is simply equivalent to // in Javascript, it is used to comment out one line.
It is a language feature that does not seem to be well-documented though, and might have been added for the simple reason to support this "hack". And now we have to live with it not to break backwards compatibility.
Needless to say that while this is a funny thing to know, this type of commenting should not be used in real code that other people might happen to read and work with.
The "hack" is also obsolete, because now every browser understands the <script> tag and does not display its contents (even if Javascript is turned off). Personally, in most cases I try avoid writing Javascript directly into HTML anyways and always load my Javascript as an external resource to separate HTML and Javascript.
In another StackOverflow question, #MathiasBynens gave what I believe is the answer:
Why is the HTML comment open block valid JavaScript?
In short, apparently, this is a non-standard addition to browser-based JS engines that allows these <!-- and --> as single-line comment markers like the // in standard JS.

'Sys' is undefined javascript error

I am getting the following error:
Microsoft JScript runtime error: 'Sys'
is undefined
While trying to execute:
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
//ERROR IN THIS LINE!!!
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onUpdated());
function onUpdated() {
// get the update progress div
var pnlPopup = $get("div2");
// make it invisible
}
</script>
</head>
Ensure you have a Scriptmanager on the page and the script is below the scriptmanager. Maybe try to put your script tag into body and see what happens.
I don't know how you can fix that that its not in the body, but maybe there is a callback from Sys when its loaded
You don't need brackets to tell which function is neeeded:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onUpdated);
If that is your full code, then the problem is that the Microsoft AJAX libraries aren't being included. If it isn't your full code, then you need to post more as it's a little hard to get beyond the fact that the library isn't included. Somewhere in your file -- prior to the line you are having problems with you need to have a javascript include like:
<script type="text/javascript" src="/scripts/MicrosoftAjax.js" />
As well as probably a few others.
Note: I'm talking about the generated source. Showing more complete markup would probably also suffice.
I'm a bit late to the discussion here, but a simple solution is to create a "ASP.NET AJAX-Enabled Web Site" in Visual Studio and then merge the web.config from that site with your existing site.
In this way you get all the configuration you need without having to carefully read through a lot of docs. Speed is of the essence and all that :)
You can fix this issue by setting the EnableCDN property to "true".refer this link
I had the same issue , after all findings I found that the required script files ("MicrosoftAjax.js", "WebForms.js","jquery", "bootstrap" etc..) were not loaded properly which was causing me 3 errors "Sys' is undefined", "webform_docallback is undefined" and "webform_initcallback' is undefined", my actual problem was I had reference to these files which where not included in my propject, my bad I have not noticed it, I have tried all the possible solutions from the internet before I really found that my js files were not included and thats the reason its not loaded properly.
Hope this helps..

Categories