I am loading the java-script file in Django template:
<script type="application/javascript" src="{% static 'online-v3.js' %}"></script>
It is loading properly on Chrome. But, on Firefox I get the following warning:
The script from “http://127.0.0.1:8000/static/myscript.js” was loaded even though its MIME type (“text/plain”) is not a valid JavaScript MIME type.
I fear that due to this problem, on some browser the JS file might not load at all.
What is the possible reason for this and how can I solve this issue?
Remove the type
or change it to "text/javascript"
.
In html5 spec the type
is not required unless it is not javascript
To add onto evilpie's answer, the root cause for me was using the basic py -m http.server
for local testing. In order to correctly assign .js
files the 'text/javascript' MIME type, I instead use this script from Github user HaiyangXu.
# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver
PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map={
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'': 'application/octet-stream', # Default
}
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
Source: https://gist.github.com/HaiyangXu/ec88cbdce3cdbac7b8d5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With