Jump to content

How are you supposed to call JavaScript files using HTML?

LessThanSatisfactory

I wrote a simple line of javascript

var name = promt("What is your name?");alert("Hello, " + name);

and I wanted to mess around and see what I could do but I can't seem to call it using html

here is my line of html

<script src="JavascriptTest.js"></script>

I just started coding this so please explain in detail :)

 

Edit: Code typos

Build: Fx-8120@4.1ghz,  Gtx770@1.0ghz,  16gb Patriot memory,  Cheap case from thermaltake

Link to comment
Share on other sites

Link to post
Share on other sites

I wrote a simple line of javascript

var name = promt("What is your name?");alert("Hello, " + name);

and I wanted to mess around and see what I could do but I can't seem to call it using html

here is my line of html

<script scr="JavascriptTest.js"></script>

I just started coding this so please explain in detail :)

you wrote "scr" instead of "src" :) it stands for "source"

watch out for typos

Link to comment
Share on other sites

Link to post
Share on other sites

you wrote "scr" instead of "src" :) it stands for "source"

watch out for typos

I fixed it but still nothing happens...

Build: Fx-8120@4.1ghz,  Gtx770@1.0ghz,  16gb Patriot memory,  Cheap case from thermaltake

Link to comment
Share on other sites

Link to post
Share on other sites

Well you have included the file just fine... but you spelled prompt wrong.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Well you have included the file just fine... problem is there's nothing to call in the file; i.e a method.

 

See this fiddle I did for you: http://jsfiddle.net/fNPvf/6504/

 

And this reference: http://stackoverflow.com/questions/1947263/using-an-html-button-to-call-a-javascript-function

You don't need a function, anything not included inside a function should autorun when it is imported.

 

Are you sure the filename/path is correct?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You don't need a function, anything not included inside a function should autorun when it is imported.

 

Are you sure the filename/path is correct?

 

Ya I released, corrected it before you posted that. Thanks. :D

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I fixed it but still nothing happens...

you can just check the javascript console (usually ctrl+maiusc+J, or in some sort of developer menu of your browser of choice)

the console will complain that there is no "promt" function, which is kind of legit

Link to comment
Share on other sites

Link to post
Share on other sites

It works! Thanks, one more question though. Could you explain to me what this means?

if (name != null)

I understand if-then statements in javascript but I don't get the parts in the bracket

Build: Fx-8120@4.1ghz,  Gtx770@1.0ghz,  16gb Patriot memory,  Cheap case from thermaltake

Link to comment
Share on other sites

Link to post
Share on other sites

It works! Thanks, one more question though. Could you explain to me what this means?

if (name != null)

I understand if-then statements in javascript but I don't get the parts in the bracket

 

Should be (http://jsfiddle.net/fNPvf/6520/):

if (prompt("What is your name?")){    alert("Hello, " + name);}

Simply checks that there was something entered. Forgive me it's early here and I got little sleep!!!

 

As @Ciccioo said remember to check your console output.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Should be (http://jsfiddle.net/fNPvf/6520/):

if (prompt("What is your name?")){    alert("Hello, " + name);}

Simply checks that there was something entered. Forgive me it's early here and I got little sleep!!!

 

As @Ciccioo said remember to check your console output.

 

Correction:

var name;if (name = prompt("What is your name?")){  alert("Hello, " + name);}
Link to comment
Share on other sites

Link to post
Share on other sites

 

Correction:

if (var name = prompt("What is your name?")){  alert("Hello, " + name);}

 

LOL sorry! Think I'll just go back to bed  :D  :rolleyes:

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

LOL sorry!  :D  :rolleyes:

 

Even my correction was wrong, actually. Turns out you can't declare a variable inside a boolean condition.  :P 

I have updated it now.

Link to comment
Share on other sites

Link to post
Share on other sites

We will get there eventually :)

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

You should also change your javascript code style.

In this case it doesn't matter but you should never put the "{" in a new line, because javascript has

automatic semicolon injection which could lead to errors that are very hard to track down.

 

See this blog post for an explanation: http://remysharp.com/2007/11/21/javascript-style-why-its-important/

 

 

EDIT: Oops, I did not mean to put "}" in there.

Link to comment
Share on other sites

Link to post
Share on other sites

You should also change your javascript code style.

In this case it doesn't matter but you should never put the "{" or "}" in a new line, because javascript has

automatic semicolon injection which could lead to errors that are very hard to track down.

 

See this blog post for an explanation: http://remysharp.com/2007/11/21/javascript-style-why-its-important/

 

This is an awesome point, another example here.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

You should also change your javascript code style.

In this case it doesn't matter but you should never put the "{" in a new line, because javascript has

automatic semicolon injection which could lead to errors that are very hard to track down.

 

See this blog post for an explanation: http://remysharp.com/2007/11/21/javascript-style-why-its-important/

 

 

EDIT: Oops, I did not mean to put "}" in there.

 

Why does Javascript have semicolon injection in the first place?

Link to comment
Share on other sites

Link to post
Share on other sites

Why does Javascript have semicolon injection in the first place?

i found it pretty absurd, but with it being a 'web language', and with browsers trying to be as permissive as possible with html and css, i'm not too surprised that javascript follows the same rule: "if there is some code, the browser should somehow execute it. no matter if it's correct or not. if it's incorrect, try to make it somehow correct and execute it"

with that said, i think that semicolon injections really shouldn't be a thing, and they even call it a feature

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×