Super Nooby Question about javascript?
Moderator: General Moderators
-
PHPHelpNeeded
- Forum Commoner
- Posts: 83
- Joined: Mon Nov 17, 2014 8:03 pm
Super Nooby Question about javascript?
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?
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?
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.
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.
-
PHPHelpNeeded
- Forum Commoner
- Posts: 83
- Joined: Mon Nov 17, 2014 8:03 pm
Re: Super Nooby Question about javascript?
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?
You tried to access the file directly. Try including it in your markup like so.
When you access the HTML page, the JS will load.
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>-
PHPHelpNeeded
- Forum Commoner
- Posts: 83
- Joined: Mon Nov 17, 2014 8:03 pm
Re: Super Nooby Question about javascript?
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.Celauran wrote:You tried to access the file directly. Try including it in your markup like so.
When you access the HTML page, the JS will load.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>
Re: Super Nooby Question about javascript?
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?
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.
-
PHPHelpNeeded
- Forum Commoner
- Posts: 83
- Joined: Mon Nov 17, 2014 8:03 pm
Re: Super Nooby Question about javascript?
It has to run server side right?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.
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?
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.
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.