Numeral.js File Not Found - javascript

I am following the instruction to use Numeral.js.
I insert <script src="numeral.min.js"></script> before the end of my <body>.
when I reload the page i get the following error:
GET file:///<project_path>/numeral.min.js net::ERR_FILE_NOT_FOUND
And if I use numeral(1000).format('0,0') I get ReferenceError: numeral is not defined
same problem if I use <script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>:
GET file://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js net::ERR_INVALID_URL
What am I doing wrong? how do you use numeral.js?

Try to either :
specify https:// instead of // at the beginning of the cdn url
copy numeral.min.js next to your html file on disk
// means "use the same scheme as current page"
If you opened your html file by opening the file from your disk in your browser, it's url will be a file:// url, hence the error when you tried accessing the file with //<cdn>/...
An alternate way is to start a webserver on your machine and access your page through http(s)://localhost:someport/my test.html

Related

Loading jquery script from CDN works, but loading same script from IIS yields illegal character?

In my static webpage loaded from IIS (v8.5.9600), I include jquery with the following:
<script type="text/javascript" src="js/jquery.min.js"></script>
And when loading the page in the firefox browser I get the following error in my console:
Uncaught SyntaxError: illegal character [Learn More] jquery.min.js:1
If I load it from the CDN, everything is fine, my code runs and I get no errors (my code is in the html page and thus not included from another script).
<script type=“text/javascript” src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js">
I downloaded the file from the CDN and copied it directly into IIS, but when it loads I still get the error in Firefox about an illegal character.
Both of the files from the CDN and IIS load up, so that is not the issue, but I did notice that when the file is loaded from the CDN it looks like this:
But when I load it from IIS (v8.5) it looks like this:
P.S. I tried loading a simple script from IIS (not from the CDN) as well and I get the same error. Is it something to do with IIS's settings?
Check the browser network tab snapshot which shows the script file name and make sure it loads up on its own from the URL you specfied in the src attribute of your <script> tag.
Your Javascript <script> include tags need to have the charset="utf-8" attribute so that the browser requests the script from the server in the utf-8 encoding.

404 Error: localhost MAMP server cannot find javascript file

I am coding a PHP website homepage using a MAMP localhost server. I would like to connect the main HTML file to a javascript file, but when I do so, none of the javascript is executed, and an error that says Failed to load resource: the server responded with a status of 404 (Not Found) appears.
I have tried using the absolute file path when including the file under the src attribute (though the js file is in the same folder as the main html file anyway). I tried turning the server on and off and reloading the page several times, but the error still appears.
<head>
<script type="text/javascript" src="/homescript.js"></script>
</head>
<body onresize="changeHeaderDisplay()" onload="responsiveCarousel()">
</body>
The forward slash at the beginning of the filename in the script tag is the problem :
src="/homescript.js"
When your browser sees a forward slash, it assumes the file is at the top level of the domain it's searching. So if your html file is at:
file:///Users/Jack/test_program/index.html
it is searching for homescript.js at:
file:///homescript.js
To make it search for it at:
file:///Users/Jack/test_program/homescript.js
simply remove the leading forward slash:
<script type="text/javascript" src="homescript.js"></script>
Edit: On a website, it's the same phenomenon:
With leading slash:
http://www.example.com/homescript.js
Without leading slash: http://www.example.com/test_program/files/homescript.js

jquery error on html page

I have a json data on localhost. I want to add it to pie chart. My html page is ready but I have an error about jquery. When I open the html page console says me, "js/jquery.js" file not found. My file is here i controlled it
jquery.js
And this errorError: "Failed to load resource: net::ERR_FILE_NOT_FOUND"
How can i do against that problem? Thank you
you must specific location folder JS
example: (in folder : www/your_app/js/)
to test bugs? click right in your browser and view source, and click again your js/jquery.js, if not found? you was wrong link into folder JS
It sounds to me like you have either an error in the path to the file or the file does not exist.
Try the opening the link to your jquery file in your browser to see if it is there. If it is there, make sure the link to the file does not have any spelling mistakes and that you are not missing any directories
You don't need type="text/javascript" anymore in HTML5, concerning your file, it does not exist or the path is wrong so verify this by entering the source address in your browser to check that or simply copy and paste the relative path to be certain it is alright.

Include a JavaScript library in a JSP file

I'm trying to use JavaScript functions from the a JavaScript library in my JSP file to display the result on a web-browser page, but it seems like the inclusion didn't work.
I actually put the .js file corresponding to the library in the WEB-INF folder and added the following line in the JSP file to include it in it :
<script type="text/javascript" src="./jsgl.min.js"></script>
I successfully managed to use the library in a simple HTML file, that's why I don't understand why this doesn't work.
EDIT :
TLDR
Put the JS file in a folder under web content (but not WEB-INF) like [WebContent]/js/jsgl.min.js, and use the following in the JSP:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jsgl.min.js"></script>
Explanation
JSP files are compiled by the server, then processed to send data (typically HTML) back to the web browser. A <script> tag is a HTML tag that gets interpreted by the browser, not by the servlet container. So the browser sees that in the HTML then makes a new request to the server for the JavaScript file in the src attribute.
The src attribute is relative to the URL that the browser asked for, not to the path of the JSP on the server.
So as an example, let's say:
The browser asks for a page at http://example.com/SomeWebApp/some-resource
The servlet container internally forwards the request to a JSP at /WEB-INF/jsp/somepage.jsp
The response sent to the browser contains the script tag <script type="text/javascript" src="./jsgl.min.js"></script> (as in your question)
The browser sees the URL ./jsgl.min.js and resolves it relative to the URL it has asked the server for (which in this case was http://example.com/SomeWebApp/some-resource - note there is no trailing '/') so the browser will request the JS file from http://example.com/SomeWebApp/jsgl.min.js*. This is because the relative URL in the script tag's src attribute starts with a '.'.
Another answer suggested putting the JS file in a 'js' folder and changing the script tag to <script type="text/javascript" src="/js/jsgl.min.js"></script>. Using the same original page URL as in the example above, the browser would translate this src URL to http://example.com/js/jsgl.min.js. Note that this is missing the "/SomeWebApp" context path.
The best solution therefore is indeed to put the JS file in a static folder like /js/jsgl.min.js, but to use the following in the JSP script tag:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jsgl.min.js"></script>
The JSP will translate the ${pageContext.request.contextPath} bit into the current context path, making the code portable (if you redeploy the webapp with a different context path, it will still work). So the HTML response received by the browser will be (again, sticking with our example above):
<script type="text/javascript" src="/SomeWebApp/js/jsgl.min.js"></script>
The browser will now resolve that relative URL to the correct target.
__
*If the original URL had a trailing slash = i.e., was http://example.com/SomeWebApp/some-resource/, the JS URL would be http://example.com/SomeWebApp/some-resource/jsgl.min.js
Static resources should be put outside the WEB-INF folder (as you would typically not allow web access to its content).
You could put the file under webapp/js/, then change your script import to:
<script type="text/javascript" src="/js/jsgl.min.js"></script>
In addition to being good practice, this is good as it is not relative to the location of the JSP file.
Files in WEB-INF are inaccessible.
You may put them under webapp and try accessing as mentioned above.

jQuery CDN is not loading on LocalHost

I have a jquery cdn loading from the following:
<head>
.
.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
then in the body I have my source for my script
<body>
.
.<script src="app.js"></script>
</body>
This is all on local, but when I view in browser I keep getting the following errors in console:
GET file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
Uncaught ReferenceError: $ is not defined
I assume this is saying the jQuery function "$..." is undefined because there is an error in GET of the CDN, why would this be happening on local?
You aren't actually running on localhost (http://localhost) but on the local file system (file:///path/to/whatever.html). The protocol is copied with the // link to file://ajax.googleapis.com which of course doesn't exist.
You should set up a server on your computer so that you'd have an actual localhost accessed with the http protocol. It would have other benefits as well since browsers act a bit differently also in other regards when the page is loaded directly from the file system.
Have you tried removing the "//" at the beginning of the "src" attribute and instead using "http://"? It is probably appending "localhost" to the beginning of the URL because of those slashes.
I've answered a similar question in How to use Bootstrap CDN? and, as Juhana said, the problem is the missing protocol when loading jQuery.
The protocol-less CDN works when you are using http or https, because browsers will add the prefix to the jQuery URL with it.
But when using a local html file, the missing protocol will be file: and browsers will not be able to find jQuery in something like file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js failing to load it. In that case you must add the protocol manually, which is a pitty if you finally use the page on-line because a protocol-less URL is better and more cache friendly (http://encosia.com/cripple-the-google-cdns-caching-with-a-single-character/).

Categories

Resources