How to refer javascript file in html present in another folder - javascript

I have a JS file in folder
/admintool/src/main/webapp/static/js/itemInventory.js
i have html file in folder
/admintool/src/main/webapp/WEB-INF/templates/itemInventory.ms
On page load my starts with url - http://localhost:8080/admintool/selectItem/showAllItems
Inside itemInventory.ms file, when i use the below code to refer that javascript, it is not able to detect it.
<script src="static/js/itemInventory.js"></script>
In browser console, i get an error
GET http://localhost:8080/admintool/selectItem/static/js/itemInventory.js net::ERR_ABORTED
How do i specify the location of this JS file properly ?

Are you sure, that the links are right? The link from the HTML to the JavaScript have to be:
../../static/js/itemInventory.js
Ivo

Related

JavaScript path doesn't load on Live Server extension (VS Code)

HTML and CSS files are working perfectly on my live server. But every time I lead to a .js script it will not be shown on my live server. If I try to load the .js file directly through the URL it shows "Cannot GET /line.js". I already tried out everything I've found on the internet but it's still not working. Here are the points I checked/did:
Installed Code Runner
Installed Node.js = node.js system path done
Settings = Live Server Config = specified browser
"liveServer.settings.CustomBrowser": "chrome" on JSON settings
.js file is in a separate folder and accessed via <script src="line.js"></script> on index.html
Chrome is set as default browser on my system
Thanks for your inputs.
If the js file is in a separate folder, you need to provide the exact route to the folder in the script tag, since in the current form it is trying to find the js file in the root directory. The script tag should look like this:
<script src="FOLDER_NAME/line.js"></script>
It's possible that your javascript file is being loaded before the HTML page is rendered. You can try adding "defer" to your script tag like this:
<script src="demo_defer.js" defer></script>

Why can't I get my script tag src's to work?

I have been coding up a localhost, and I made the localhost by using of course a JavaScript file to do so, and I then made it reference an HTML file. However, I noticed that when I am using localhost to serve up the HTML file I get this error:
"GET http://localhost:3333/filetesting.js"
The filetesting.js is that js file, there are also other things I'm referencing too, like websites. I'm referencing it by using script tag src.
I looked at the network on developer tools of it and it says it's a 404 error not found. I'm trying to figure out how to reference my script tag src's without having localhost:3333 go before it.
When I run the HTML file without using the localhost, it works just fine when it comes to the script tag src's. If you do not entirely understand what I'm asking for, just ask.
Assuming that your script will always reside in the root level of your website, you can simply target it with the root-relative prefix /:
<script src="/filetesting.js"></script>
This will load your script from the root, regardless of the site the file is hosted on. For example, on http://localhost:3333/ it will load the file from http://localhost:3333/filetesting.js, and from http://localhost:3333/folder/, it will attempt to load the file from the same location.
If you move your files over to a proper website, it will still work the same way: www.example.com will look for the file at www.example.com/filetesting.js, and www.example.com/folder/ will look for the same file at www.example.com/filetesting.js.
Hope this helps! :)

Refrer javascript inside html file?

I have below folder structure.
inside: webapp/WEB-INF/some.jsp i have
i have javascript file in the same webapp/WEB-INF/js/myform.js location
i referred it in some.jsp as below:
<script type="text/javascript" src="js/myform.js"></script>
But it is not finding javascript file. in viewsource i am getting below lines:
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
<p>RequestURI=/js/myform.js</p><p><i><small>Powered by Jetty://</small></i></p><br/>
is my javascript link correct in jsp file?
Please correct me.
Thanks!
The WEB-INF folder is secure, meaning you can't access resources placed in it directly using a URL from the browser.
To work around it, place the JS under the webapp/js directory.
webapp/WEB-INF is not the root of your application, put your dir js in webapp rather than in webapp/WEB-INF

PHP code-igniter

I'm having troubles calling my external js files from my views php pages. The js files are saved in the "resources" folder in the root folder i.e "root/resources/scripts/myfile.js" just like the "root/application" and the "root/system" folder.
so in one of my php files in the views folder i am using this code:
<script type="text/javascript"
src="<?php echo base_url();?>resources/scripts/welcome.js">
and this php file is located in "root/application/views/welcome.php"
and still my page cannot link with the java script so i am stuck big time.
regards
k..
Try this:
<script src="<?= base_url('resources/scripts/myfile.js')?>"></script>
Looking at your question and the comments below your question, it seems you are using "resource" in one example and "resources" in another example.
Once fixed, view the source of the page (I use FireFox, as it makes the links clickable), search for "welcome.js". Once found, click on the link for that file. If you see javascript, it means the file is found, alternatively you will see a 404 error.
First of all check if the resource folder is available,
The java script file is available and the file naming is in the same small or capital case, and server has public permission of the file and folder.
by the way your code has missing end tag
</script>

External Javascript File - QueryString Info Available?

On an HTML page I have a reference to an external Javascript file like this:
<script src="http://MyServer.com/js/myscript.js?Happy=True"></script>
Inside the myscript.js when it runs, can I get the Happy=True QueryString-like part of the js source URL?
Note I do not want the URL of the HTML page, I need to get the URL of the js file.
My Guess is no.
You will be able to detect the src-attribute of the script-element(would be easier to locate if the <script> has an ID). Out of that URL you could extract the Query-String.

Categories