Page 1 of 1

Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 5:00 am
by PHPHelpNeeded
Question#1: this is the first time I will start doing javascript programming, do you have to install javascript like you do PHP?

Question#2: when I write a javascript, script in the HTML file, which file extension is the file going to be? can it stay as *.HTML and still run javascript? or which file extension does javascript need to run?

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 7:07 am
by Celauran
The JS you'll most likely be writing now runs client side, so there's nothing for you to install. Server side JS does exist through things like Node.js, which does require an installation. I wouldn't worry about that just yet.

You don't want to write JavaScript in the same file as your HTML; save it to its own .js files and include them through the script tag.

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 7:42 am
by PHPHelpNeeded
I did a file like main_menu.js and typed its url in the browser address bar, but the browser downloaded the file instead of running, so I am confused, what did I missed?

Code: Select all

            http://localhost/dashboard/menus/main_menu.js

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 8:17 am
by Celauran
You tried to access the file directly. Try including it in your markup like so.

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript" src="//localhost/dashboard/menus/main_menu.js"></script>
</head>
<body>
	
</body>
</html>
When you access the HTML page, the JS will load.

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 11:19 am
by PHPHelpNeeded
Celauran wrote:You tried to access the file directly. Try including it in your markup like so.

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript" src="//localhost/dashboard/menus/main_menu.js"></script>
</head>
<body>
	
</body>
</html>
When you access the HTML page, the JS will load.
Are you saying that those lines of are supposed to be inside the same js file that I tried to access that it will not get download, let me try it.

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 11:34 am
by Celauran
No. I'm saying you don't point your browser directly to the JS file. The JS gets called from within your HTML somewhere. Include it in the HTML, navigate to the HTML page, JS will load.

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 11:40 am
by Weirdan
As far as the browser concerned, javascript always run in a context of some html page (apart from browser extensions, but let's not get there just yet). So you would have an html page (e.g. index.html, with the code that @celauran posted) that references your javascript file. When you visit that page your javascript file will be loaded and its code get executed.

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 11:44 am
by PHPHelpNeeded
Weirdan wrote:As far as the browser concerned, javascript always run in a context of some html page (apart from browser extensions, but let's not get there just yet). So you would have an html page (e.g. index.html, with the code that @celauran posted) that references your javascript file. When you visit that page your javascript file will be loaded and its code get executed.
It has to run server side right?

How can I load if I open an html page that has that file as a source in the script tag if the file is located at the server and I might open the html file client side at an outside computer (outside I mean outside the network, like from the library or my college computer room or my brother's house)?

Re: Super Nooby Question about javascript?

Posted: Fri Nov 28, 2014 12:00 pm
by Celauran
The short answer is you can't.

Typically, you'll be accessing the HTML across a network, so the JS being on the server doesn't matter. Server sends file to client, client parses the file. If you're going to be accessing HTML files locally (ie. via file:/// rather than http://) then you'll need the JS to be local as well.