0

I have this HTML code

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MyPage</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="/scripts/FileFunctions.js"></script>
</head>
<body>
</body>
</html>

and this "FileFunctions.js" file

function addField(){
  $('form input:file').last().after($('<p><input type="file" name="files[]" id="file" /><br /></p>'));
};

but if I load the page, my file "FileFunctions.js" is not loaded... The javascript console says

Uncaught SyntaxError: Unexpected token < FileFunctions.js:1

but I can't find out what is wrong with these codes... Thanks for any ideas

7
  • is the slash(/) before the scripts/FileFunctions.js is your intension? just to be sure Commented Jun 27, 2013 at 21:20
  • 2
    My guess is your web server isn't serving the file you expect and is serving a 404 Not Found page full of HTML content instead of JavaScript code. Have you used Firebug to ensure the file you're downloading is the file you're expecting to download? Commented Jun 27, 2013 at 21:22
  • Have you tried using JSLint? jslint.com Commented Jun 27, 2013 at 21:22
  • @RichardJPLeGuen It shouldn't matter if it's a 404. The browser won't attempt to parse it, and therefore won't throw an error Commented Jun 27, 2013 at 21:23
  • @jackchuka yes it's my intension Commented Jun 27, 2013 at 21:27

2 Answers 2

2

The problem was with wrong configuration in .htaccess. The apache returned my HTML page with info about non existing "scripts.php" file instead of "/scripts/FileFunctions.js".

Solution is simple, I've just added RewriteCond to .htaccess

RewriteCond %{SCRIPT_FILENAME} !scripts/(.*)$
RewriteRule ^([a-zA-Z0-9]+) index.php?page=$1 [QSA]
Sign up to request clarification or add additional context in comments.

Comments

0

I think there is an issue with your javascript. Try the code below

JS Fiddle: http://jsfiddle.net/jonocairns/Z2C9Y/1/

"use strict";
function addField() {
    $('form input:file').last().after('<p><input type="file" name="files[]" id="file" /><br /></p>');
};

4 Comments

it's working without your "use strict" as well! See jsfiddle.net/Z2C9Y/2
I would strongly recommend always using "use strict". It makes development and debugging much easier! :)
Yes! Quote from MDN -> "Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript." MDN
Thank you for answer but I've already solved it - @Richard JP Le Guen gave me nice hint that solved my problem :) I had badly configured .htaccess that returned html file instead of jscript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.