3

When trying to dynamically load a Javascript file using jQuery I keep getting a "not well-formed" error message. I have found people with similar problems on here but have yet to see a resolution provided.

My main script uses:

$.ajax({
    url: 'test.js',
    dataType: 'script',
    cache: true,
    success: loadScriptReturn
});

function loadScriptReturn() { }

My dynamically loaded script (test.js) in its simplest form:

alert('Hello World.');

Since I am specifically loading this as a script MIME type it eliminates the possibility that Firefox is confused as to what type of file is being pulled in. Is there a way to solve this problem? Alternatively, is there a way to shut off this specific error in Firefox? (note: this is an error, not a warning, which is extremely annoying because I do want to see subsequent error messages -- bad on Firefox as this should have been a warning, not an error)

Keep in mind, this example WORKS, but it still produces an error. Given how many scripts I need to load dynamically it will be tedious trying to sort through "real" error messages if I can't find a way to get rid of this.

Thank you in advance to contributors!

OrangeFrog
  • 285
  • 4
  • 12
  • I doubt this is Firefox giving you this message. More likely Firebug. – Mark Fraser Apr 03 '12 at 15:29
  • Yes, probably Firebug - in addition, you should not be using `ajax()` to fetch JS script - you should be using `getScript()` http://api.jquery.com/jQuery.getScript/ - While one is shorthand for the other, it is more clear. – Dutchie432 Apr 03 '12 at 15:31
  • Also check your encoding. If you are using UTF-8, make sure that you arn't using a [BOM](http://en.wikipedia.org/wiki/Byte_order_mark) because this can cause all sorts of headaches. – mekwall Apr 03 '12 at 15:32
  • You're right, Firebug error, but I did find a solution (see comment below)... thanks everyone for checking into this. I spent 3-4 hours yesterday and 1-2 hours today trying to figure this out. Turns out this is only thrown when using Ajax locally; remotely: no error. – OrangeFrog Apr 03 '12 at 16:56

2 Answers2

2

I found several questions that may help you:

The general consensus is that you need to change the MIME type to application/json.

Community
  • 1
  • 1
Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
  • 2
    This error message is only thrown locally when dynamically loading a script. The minute you start working off a remote server the error message does not appear. As far as I can tell Firefox doesn't see the MIME when loading a local file (bug?). If you're someone out there who is receiving this same error message, try uploading all your files to a remote server to see if it goes away... I spent 3-4 hours trying to diagnose the problem... hopefully this saves someone else some time. – OrangeFrog Apr 03 '12 at 16:54
1

A better way to load script dynamically:

$('head').append('<script type="text/javascript" src="test.js"></script>');
ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
  • I tried your code (thanks for that by the way) but I still recieved the same error message. Interestingly though, I found a solution while attempting your code. – OrangeFrog Apr 03 '12 at 16:52
  • 1
    This error message is only thrown locally when dynamically loading a script. The minute you start working off a remote server the error message does not appear. As far as I can tell Firefox doesn't see the MIME when loading a local file (bug?). If you're someone out there who is receiving this same error message, try uploading all your files to a remote server to see if it goes away... I spent 3-4 hours trying to diagnose the problem... hopefully this saves someone else some time. – OrangeFrog Apr 03 '12 at 16:55
  • just te be sure, try to wrap your js file by //<![CDATA[ -- your JS code -- //]]> I think this may fix the error! – ilyes kooli Apr 03 '12 at 17:04