Can a single javascript file be used by multiple html files? - javascript

I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html
I want to access the javascript file main.as in all files .
I have used in all of the five but I'm not able to access it . Is it possible to share the .js file among the html files.
Plz reply,
Thanks in advance
Prashant Dubey

Yes, it is entirely possible. That's the point of being able to have a JS file instead of having to embed all the code in the HTML.

yes this is well possible. You merely have to include
<script type="text/javascript" src="main.js"></script>
in your HTML files, preferably near the bottom (for faster rendering). What have you done so far?

Yes. Totally possible.
Just reference it in all of the files e.g. by
<script type="text/javascript" src="Scripts/main.js"></script>

Yes, it is possible.
Probably there is something wrong with the way you access the javascript from your html. Show us the <script ...>...</script> part of your html.

Yes. Are you using the correct path to the main.js file in your html files?

Create separate javascript file with .js extension with all your function in it and
just include this javascript file in the head tag of all the html scripts u wanna use that in.
Like::
<html>
<head>
<script type="text/javascript" src="JavaScriptFilePath.js"></script>
</head>
<body>
<!-- use javascript -->

It can happen both ways..
a single html file can use multiple javascript file
2.a javascript file can be used in several html files.
In first case javascript file load can be conditional based on location, user preferences, time, age group, content restriction.
You can see good example when facebook loads its page. I loads number of javascritps.

Related

Should external javascript file start and end with <script> </script>?

I want to use jquery in my web application. Should I include it as a link in my external JavaScript file or inside my html file?
Thanks in advance
No. The way to include a javascript file is using <script src="file.js"></script>
The contents of file.js should not be surrounded with <script> tags as that is html code.
Yea sorry i was mistaken, didn't think external javascript counts but apperantly it does.
<script src="myScript.js"> code in here </script>

Extract javascript from JSP to JS file

I'm trying to refactor parts of our front-end at the moment, using Intellij. We have a lot of javascript that is within our JSPs. I'd like to be able to extract from the JSP into a .js fil and replace it with a script tag referencing that file. I know how to extract to an 'includes' file, but I'd like a way of extracting and replacing the code with a proper script tag.
Does anyone know of a plugin that can accomplish this quickly and easily? (Or a pre-existing function in Intellij that I'm unaware of)
Please vote for this issue (it's 5 years old and had zero votes).
just create one file with .js extension copy and paste your js codes with their function names. then include that file in your jsp project header section.
eg:
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
thats all..

linking code from a separate document to work on another page

I have taken script from a webpage document I have made and I have saved onto a notepad document with the extension .js.
I would like to now know how I can reference this .js file from the current page that I have created so that the script will run on that page without the actual code being there, just the reference link.
Something like this?
<script type="text/javascript" src="myfile.js" />
It must be a reference link. There are other techniques besides the standard, but they all rely on linkage. You can't beat the linkage. You can't stop the linkage. You mus succumb to the linkage.
That's not how the web works? You might be able to use some sort of developer tool to execute arbitrary javascript from a file when the page loads, but that would just be overriding the default way the web works.
you will need to do :
<script type="text/javascript" src="URI_TO_DIR/extension.js"></script>
see this page about embedding a javascript file
if script is on your computer, it will of course not be accessible on the web, for that you'd need a webserver or a webspace
You can include an external JavaScript using a script tag with a src attribute. For example, something like
<script type="text/javascript" src="javascriptfile.js"></script> should do what I think you're asking for.

Make one all.js from various js files

I am making a website using PHP. I have various Javascript snippets in various pages and various Javascript files. I want to put them all in one .js file. How can I do that?
Copy them all into one file, in the order they were included within document.
If they were written correctly, there should be no problems. But there may be problems regarding some conflicts (like names of the variables) or cases, when the script was not meant to be executed on all pages (eg. assumes some container exists within HTML, but this container is only on some pages - thus on other pages the script may throw some errors or behave inappropriately).
With PHP you could just make a faux-javascript file, e.g. js.php and then include all the js files like:
<?php
include('foo.js');
include('bar.js');
Then reference this file from the main php page's html:
<script type="text/javascript" src="js.php"></script>
Another way is by inline js as
<script type="text/javascript">
<?php
include "one.js";
include "two.js";
?>
</script>
I also try to reduce js, css and use image sprite for background to reduce browser header requests.

Adding Jquery to root folder in my server (localhost)

I am learning web development and I have no idea where to place jquery.js file on my localhost server. Do I just save it as such /var/www/jquery.js and then reference it with:
<script type="text/javascript" src="jquery.js">?
Will that work or is there a particular place that is called a root folder on localhost server that I don't know about...Sorry for how dumb this sounds..
Just put it anywhere, and include a <script> tag that embeds it, e.g. <script type="text/javascript" src="/some/location/in/my/webroot/jquery.js"></script>.
bob,
Its always good to maintain nice heirarcy structure
do as below
Create a separate folder for js , which is javascript and place all of them there
root/js/jquery.js
and you can include
/js/jquery.js
This should be good
either you can include from google cdn also , so that you can have better performance
http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
reference here
http://softwareas.com/google-jquery-cdn
You can place it anywhere you want as long as the includes contains the proper path.
If you place it here:
/www/ajax/jquery.js
Your includes would look like this:
<script type="text/javascript" src="/ajax/jquery.js"></script>
BTW: /www/ is your "web root"
EDIT:
I'd also place my JavaScript includes and scripts at the end of my body section...
Click here to read about script location and performance.

Categories