jquery.js call from HTML syntax error - javascript

I have an HTML page generated by an objective-C application and I want to load and open it in a local directory (not in a web server). The javascripts jquery.js and jquery.plot.js are stored in the same directory before the page is loaded.
I made the HTML page by following closely the examples available on http://flot.googlecode.com/
The scripts are declared in the HTML page as follows :
<script src="jquery.js" language="javascript" type="text/javascript"></script>
<script src="jquery.flot.pack.js" language="javascript" type="text/javascript"></script>
When I open the page, the expected graph (generated by the scripts) doesn't show up and I have the following syntax error in the javascript console :
Uncaught SyntaxError: Unexpected token ILLEGAL
The script source in the javascript console is made of strange (chinese ?) ideograms which are obviously "unexpected token" if it is what the browser sees.
I have the same problem with Chrome, Firefox and Safari.
The problem is not systematic because sometimes, it works !.
I have made tests with files prepared manually and again it works sometimes only.
I have also copied the same file which didn't work in another directory where the jquery scripts were present and, surprise, it worked ! but not always...
I have exhausted all my ideas on this problem and your help will be very much appreciated

Did you copy the JavaScript from an external source? I know I have had problems when copying from JSFiddle for example. Try removing any whitespace and see if that will get rid of the illegal token error.

I just had this problem and it was because the server on which my javascript was being hosted wasn't configured to send the javascript file with the correct MIME type. See this question.
Instead of fixing my server, I just opted to use a CDN path from cdnjs.com:
//cdnjs.cloudflare.com/ajax/libs/flot/0.7/jquery.flot.min.js

Related

net::ERR_FILE_NOT_FOUND, no typos and files are in the same directory

I have an index.html file with <script src=”sc2.js”></script> in the head section. In the same directory I have file sc2.js that has some really simple JavaScript code I wanted to test to see what it prints. The html displays correctly in my browser, but the console says:
index.html:8 GET file:///Users/myname/projectfile/%E2%80%9Dsc2.js%E2%80%9D net::ERR_FILE_NOT_FOUND
Am I missing something to make it possible to run the code in my browser? I can still run old JavaScript projects in the browser with no problems. I also tried to move the script tag around and add the full path, which made no difference. The code works in jsfiddle.net. I'm using Chrome Canary.

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.

javascript stopped working but works on diffrent server

I need your help with a strange problem.
The company I work for has a company website.
I have been updating pages on their website the last couple of days.
After updating, I decided to upload the website,
but suddenly the javascript doesn't work anymore.
I get the following error when I press f12
Uncaught SyntaxError: Invalid or unexpected token
base.js:1 Uncaught ReferenceError: $ is not defined
at base.js:1
On the test server (my own server) it works without any problems.
On the main server (company server) it doesn't work.
What could be the issue for this?
It's not on just one page, it's on every single page that the Javascript doesn't work.
[update]
I know about the different versions of Javascript.
This is already fixed in the testing environment of the company website.
This still doesn't fix the current problem.
Thank you
Wesley
On the test site you use jQuery 3.3.1:
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
On the main site you use jQuery 1.10.1:
<script src="https://www.aska-ltd.jp/js/jquery-1.10.1.min.js"></script>
Please both use v3 if you don't have specific needs.
It looks like you are using jQuery for your JavaScript code.
The header of the first page contains a tag for loading jQuery:
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
But on the second page, you never load jQuery.
As I don't know how the file structure of your server is, I cannot tell you the exact path, but you need to make sure you load jQuery in a similar way on your company website.
EDIT:
I noticed that you load jQuery from https://www.aska-ltd.jp/js/jquery-1.10.1.min.js. However, this file is not the original file (compared to https://code.jquery.com/jquery-1.10.1.min.js using MD5). If you want to use this old version (not recommended), you can try to re-download it or simply load jQuery in its latest version from its official server (recommended).
As #NoobTW and #SapuSeven have said, there is an error in the jQuery script on the production site.
On the production site, try replacing this <script src="https://www.aska-ltd.jp/js/jquery-1.10.1.min.js"></script>
with this <script language="JavaScript" type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
Also, if you load jQuery from a CDN, it may help simplify things. Here is one option for jQuery 3: <script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
See here for more options: https://code.jquery.com/
JQuery never loads at all on the main server. I tried taking the code from the file on the server and evaluating it, but discovered some sort of corruption in the JQuery file your server is returning:
If the same thing happens with an updated version of JQuery, check whether it’s also corrupted. It may be some sort of transformation the server is incorrectly performing.

WordPress JavaScript 403 Error

I wrote a real quick javascript file in NotePad++, and I am scratching my head about this 403 error I'm getting as I try to load it. Note that everything else associated with my WordPress install and the various plugins I have acquired are working perfectly. I also have yet to use any security plugins since I'm currently running my website in WAMP localhost. I changed the file to 755 using Cygwin's chmod, but that didn't help. The file is currently in my theme's directory, and I am trying to embed it into the bottom of a single page. The methodology I am using is described here (https://codex.wordpress.org/Using_Javascript), and, specifically, this is all I have done:
<script type="text/javascript" src=/mysite/wp content/themes/RainForest/slider.js"></script>
<script type="text/javascript">
<!--
test();
//--></script>
I also tried replacing the "slider.js" with another javascript file that is 200 when ran inside a PHP file, and it too got a 403 error when I tried to run it inside the content of a page. Has anyone else ran into this problem? What, if anything, could I be missing here?
You have a space in wp content instead of a dash like wp-content. You are also missing the starting double quote:
src="/mysite/wp-content/themes/RainForest/slider.js"

Javascript running from html but not php

I've read a similar question here, but can't seem to get around it in this case, so would appreciate any clarification. I have a page in php that runs several instances of JavaScript that work on cue on localhost. However one particular instance of JavaScript (that makes an arrow hide when it scrolls beyond 10px) does not run, but when i run the same from an html version of the same php page, the arrow action works as desired. Why is this the case? Here is the code of the page in jsfiddle (exactly the same is used for respective sections in the php page.)
To clarify i have added this bit of JavaScript in the following manner at the end (after footer, before body end) of the index.php file (along with other blocks of JavaScript which work as desired for their respective targets):
<script type="text/javascript">
$(window).scroll(function () {
if ($(this).scrollTop() > 10)
document.getElementById('arr_downpoint').style.visibility = 'hidden';
else
document.getElementById('arr_downpoint').style.visibility = 'visible';
});
</script>
I have added jquery as follows (and it works for all other instances on the php page except for the arrow):
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
Lastly, there are no console errors. Would like to know why only this block of JavaScript isn't working from php, but works from HTML (while the other JavaScript instances work fine on both php and HTML), and how to find out what's wrong?
RESOLUTION: Just found that I'd erroneously added # while assigning arrowpoint id. That's why the script couldn't do anything with the arrowpoint, since <a> now had a # as part of its name. sorry about that careless oversight, for the time it may have taken to consider.
If you are trying to load the page directly from the file (your page url start with 'file://...') the problem is that the browser is trying to load jquery using the same protocol (file instead of http) because you didn't specify a protocol for the external files (the src attribute starts with '//', which mean something like "use the same protocol of the current page"). You can verify if this is the reason behind your problem, opening the browser console looking for errors like:
GET file://code.jquery.com/jquery-1.11.3.min.js net::ERR_FILE_NOT_FOUND
GET file://code.jquery.com/jquery-migrate-1.2.1.min.js net::ERR_FILE_NOT_FOUND
Uncaught ReferenceError: $ is not defined
To fix this problem, you should always run you html from a server (like Apache with PHP, as you already did) or you can write the external scripts url including the protocol (but this can cause other problems if you will run your page under https)
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

Categories

Resources