简体   繁体   中英

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

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 eg 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?

It can happen both ways..

  1. 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.

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 -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM