最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript MIME type warning on Firefox - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Aug 9, 2019 at 14:35 Ranveer SinghRanveer Singh 1611 gold badge2 silver badges10 bronze badges 1
  • I get this warning by HTML using a router (called by .htaccess file) for its loading of a .js file, not using any type attribute in the script element, but only on Firefox, not on Chrome. A bug in Firefox? – David Spector Commented Jul 11, 2021 at 11:51
Add a comment  | 

4 Answers 4

Reset to default 7

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

To fix this warning for Django debug server on Windows, add following lines in your settings.py file

import mimetypes

mimetypes.add_type("application/javascript", ".js", True)

Your server is wrongly configured and is serving .js files with a wrong Content-Type header of text/plain.

In the future Firefox might start blocking scripts with incorrect MIME types.

发布评论

评论列表(0)

  1. 暂无评论