I'm having some trouble with the following code within my Index.html file:
<SCRIPT LANGUAGE="JavaScript" SRC="clock.js"></SCRIPT>
This works when my Index.html file is in the same folder as clock.js. Both Index.html and clock.js are in my root folder.
But when my index.html is in these different directories clock.js does not load:
/products/index.html
/products/details/index.html
What can I put as the 'SRC' so that it will always look for clock.js in the root folder?
Try:
<script src="/clock.js"></script>
Note the forward slash.
Use an relative path to the root of your site, for example:
If clock.js is on http://domain.com/javascript/clock.js
Include :
<script language="JavaScript" src="/javascript/clock.js"></script>
If it's on your domain root directory:
<script language="JavaScript" src="/clock.js"></script>
The common practice is to put scripts in a discrete folder, typically at the root of the site. So, if clock.js lived here:
/js/clock.js
then you could add this code to the top of any page in your site and it would just work:
<script src="/js/clock.js" type="text/javascript"></script>
This works:
<script src="/clock.js" type="text/javascript"></script>
The leading slash means the root directory of your site. Strictly speaking, language="Javascript" has been deprecated by type="text/javascript".
Capitalization of tags and attributes is also widely discouraged.
Piece of cake!
<SCRIPT LANGUAGE="JavaScript" SRC="/clock.js"></SCRIPT>
src="/clock.js"
be careful it's root of the domain.
P.S. and please use lowercase for attribute names.
If you have
<base href="/" />
It's will not load file right. Just delete it.
As you haven't specified, I don't know if you are working with flask (Python), but if you are, you have to put the JavaScript file in a directory called static. It should look something like this:
/ProjectName
/static
clock.js
/templates
index.html
main.py
And then refer to the js file as following:
<script src="/static/clock.js"></script>
As your clock.js is in the root, put your code as this to call your javascript in the index.html found in the folders you mentioned.
<SCRIPT LANGUAGE="JavaScript" SRC="../clock.js"></SCRIPT>
This will call the clock.js which you put in the root of your web site.
Related
I'm using Aurelia CLI (v1.2.0) with webpack (v4.41.0). Running the command au build --env prod works well and all necessary files are placed in a dist folder relative to the project's root as expected. However, a problem I'm seeing is the following in the generated .html file:
...
<body aurelia-app="main">
<script type="text/javascript" src="/runtime~app.66066bc9a3f8c86e3d5a.bundle.js"></script>
<script type="text/javascript" src="/vendor.bluebird~01be3b92.3dbcbc269195ad05c865.chunk.js"></script>
<script type="text/javascript" src="/vendor.setimmediate~a1c951f6.42ef81a6d814b4bc894f.chunk.js"></script>
<script type="text/javascript" src="/vendor.process~16c59945.ef28f3259f949d41518b.chunk.js"></script>
<script type="text/javascript" src="/vendor.moment~399b027d.9b9b0283b72b7237fb27.chunk.js"></script>
...
You see the src="/file_name_here" part in these script tags is not going to work as it's looking for files at the root of the main hard disk, not relative to the HTML file. If I add src="../file_name_here" then all works fine. Am I missing a webpack configuration somewhere?
Thanks for the assistance.
in your project, you have a webpack.config.js file.
there you should find the baseUrl property. change it to whatever suits you best.
for example: I want the bundle files to be in the same directory of the index.html file, regardless of their respective path on the server. (they will not always be in the root of my server).
so I just change the default '/' to '' (empty string.)
I have error and am trying to solve it for hours.
My file structure:
-/node_modules
-/www
---bundle.js
---index.html
in index.html I have such code
<script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<script src="bundle.js"></script>
Problem is bundle.js is included fine, but ng-cordova.min.js gives 404 error
Cannot GET /node_modules/ng-cordova/dist/ng-cordova.min.js
Edit: I also tried this without success
<script src="/node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
<script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
I don't use express or express.static.
Judging by your project structure I'm betting that it is powered by Express.js. I'm also betting that the www folder was set as a static folder in the express app like this:
app.use('/', express.static(path.join(__dirname, www)));
In which case your server will only serve files located in the www folder. You need to include any client dependencies in the www folder:
-/node_modules/
-/www/
--lib/
---ng-cordova/
--bundle.js
--index.html
And then load them like so:
<script src="lib/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
UPDATE
Simply put node_modules inside the www folder, it should work fine.
The problem here is that you try to load your ng-cordova.min.js file from the index.html folder.
You should change:
<script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
For this:
<script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
You are trying to load the file from the wrong directory.
You either have to use this path:
src="../node_modules/
or this:
src="/node_modules"
in case the above directory is your root directory.
What's also confusing is that the line you commented the error in is not the line the error actually comes from. Why are you trying to link to the file twice?
//Edit: As Romain was a minute faster than me he deserves the right answer reward I'd say :P Only if it actually solves your problem ofc :D
There's an app I have been building, and as it grows, so does the <script> tag grows in the index.html
index.html
<html>
<head></head>
<body>
<script src="angular.js"></script>
<script src="a.js"></script>
<script src="b.js"></script>
...
</body>
</html>
Is it possible to include all of these <script> into an external html and then load it into index.html to use it? Like so.
index.html
<html>
<head></head>
<body>
<script src="angular.js"></script>
[**Include jsInclude.html here**]
</body>
</html>
jsIncude.html
<script src="a.js"></script>
<script src="b.js"></script>
...
If so, what are some of the proper methods of performing such a task?
I have read online that I can use Grunt or RequireJS to load JS files dynamically.
However, I wish to avoid lazy loading, I want to load all of the JS files at once on startup.
Will using RequireJS or Grunt able to achieve this? Or is there another way to achieve this?
You could use webpack or browserify to compile all of your JS files into 1 JS file.
https://webpack.github.io/
http://browserify.org/
Grunt is just a task runner, it won't be able to do this task on its own. You can set up a task in Grunt to run browserify/webpack
Yes, it should be that way only. Load app.js first and then follow other files that include controllers, factories and so on in your HTML (index.html)
I am new to JS and I've tried many different ways to try and get this to work but to no avail. (I looked at the other posts but their solution didnt work for me)
My project folder is organized as follows:
+-+ project
+-- css
+-+ js
| +-- index_js.js
+-- index.html
I have got it to work with the index_js.js file outside of the js folder by assigning the script tag src to src="index_js.js". But when I try to assign it to src="../js/index_js.js" while it is in the js folder it doesn't work and won't give me an alert. I have also tried to assign src="/js/index_js.js" but it still does not work.
My index.html is:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="../js/index_js.js"></script>
</body>
</html>
My index_js.js is:
alert("inside of the js folder");
That needs to be:
<script type="text/javascript" src="js/index_js.js"></script>
Or even:
<script type="text/javascript" src="./js/index_js.js"></script>
By specifying ../js, you're searching for a js directory which is on the same level as your project directory.
Your index.html should be like this :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="js/index_js.js"></script>
</body>
</html>
Using
src="../js/index_js.js"
in the code will look for the "index_js.js" the parent folder of "project".
../ refers to up one level.
In the given code, you are at /project/index.html
When you do ../ you are moving to the parent folder i.e /root/project/
Now, when you give
src="../js/index_js.js"
it looks for the js file at:
/root/js/index_js.js
The correct code would be:
<body>
<script type="text/javascript" src="./js/index_js.js"></script>
</body>
or
<body>
<script type="text/javascript" src="js/index_js.js"></script>
</body>
By expressing "../" you will quit your current project folder an move to the root folder. Your script then tries to find the subfolder "js" and the file in a directory that does not exist. Your correct code would be:
<script type="text/javascript" src="/js/index_js.js"></script>
Since, you have the js folder on the same directory as your index.html file, you don't need to use any backslashes.
<script type="text/javascript" src="js/index_js.js"></script>
Now make sure;
You have js folder and index.html file both in the same
directory.
The folder name js is as you have mentioned.
The file name is correctly placed i.e index_js.js and it is inside the js folder.
I'm sorry for simple question. I need determinate jquery in my project, actually when I used to insert direct link to <script>. It works:
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript">
But when I use relative path it doesn't work.
<script src="js/jquery-1.11.1.js" type="text/javascript" >
this is my directory:
Your main file and jquery file are in different folders.
Main file in scripts/ folder and jquery file in js/ folder.
Just change path to:
../js/jquery-1.11.1.js
../ directory means scripts/ folder.
So your code will be:
<script src="../js/jquery-1.11.1.js" type="text/javascript" >
Remember, file names in folders and in your code must be the same.
Hope it'll help :)
Your js folder should be in your public folder.
In your layout try this:
<?php echo $this->headScript()->appendFile($this->baseUrl().'/js/jquery-1.11.1.min.js'); ?>
Unless you have a compelling reason to use relative paths, I suggest using an absolute path. It will not break if your page moves to a new location:
<script src="/scripts/js/jquery-1.11.1.min.js" type="text/javascript" >