简体   繁体   English

JavaScript文件在最近的Chrome版本上停止工作

[英]JavaScript file stopped working on the last Chrome version

We have an old page. 我们有一个旧页面。 Recently Google Chrome (and other last version browsers) stopped showing menu , but if I hit "refresh", than Inspect Tool shows the following error: 最近,谷歌浏览器(和其他最新版本的浏览器)停止显示menu,但是如果我单击“刷新”,则Inspect Tool会显示以下错误:

Resource interpreted as Script but transferred with MIME type text/html: ".../menu.js.php" 资源被解释为脚本,但以MIME类型text / html传输:“ ... / menu.js.php”

What could be the solution for this problem, or the problem is actually elsewhere? 该问题的解决方案是什么,或者该问题实际上在其他地方?

Its because the header content type is not being set correctly by the server: 这是因为服务器未正确设置标头内容类型:

Content-Type: application/javascript

To clarify, your server should set the correct http header on the response to reflect the content type. 为了明确起见,您的服务器应在响应上设置正确的http标头以反映内容类型。

I guess in your page you have a script tag with type="text/javascript" but Chrome is saying the server is not identifying the content as javascript. 我猜您的网页中有一个带有type =“ text / javascript”的脚本标签,但Chrome表示服务器未将内容标识为javascript。

the Content-Type this is not the problem. Content-Type这不是问题。

the problem is elsewhere. 问题出在其他地方。 A problem that I see is with fancybox: 我看到的一个问题是fancybox:

jQuery from version 1.9 has removed most if not all of the depreciated methods. 1.9版中的jQuery已删除了大多数(即使不是全部)折旧方法。

$.browser was one of them. $ .browser是其中之一。 Fancybox relies on $.browser, so you can't use jQuery 1.9 or maior and your version of fancybox without the migrate plugin installed. Fancybox依赖于$ .browser,因此如果未安装Migration插件,则无法使用jQuery 1.9或maior和您的fancybox版本。

You can downgrade jQuery to a version < 1.9 or upgrade fancybox to version 2 or include the jquery migrate plugin . 您可以将jQuery降级到1.9以下版本,或者将fancybox升级到版本2或包含jquery migration插件

Because the Javascript files are actually PHP files, the server transfers the file as text/html . 因为Javascript文件实际上是PHP文件,所以服务器将文件传输为text / html I did not find this out myself. 我自己没有发现这一点。 I took it from SCRIPTONITE's answer 我从SCRIPTONITE的答案中获取了

To set a header in PHP, you can use the header() function. 要在PHP中设置标头,可以使用header()函数。 You should always set the headers as early as possible to avoid errors. 您应该始终尽早设置标题,以免出错。 You may only set headers if you have not yet sent anything to the output. 如果尚未将任何内容发送到输出,则只能设置标头。 No echo, not HTML, nothing. 没有回声,没有HTML,什么也没有。 From the manual: 从手册中:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. 请记住,必须先通过常规HTML标记,文件中的空白行或从PHP发送任何实际输出,然后调用header()。

To set the header for a Javascript file, use this line: 要设置Javascript文件的标头,请使用以下行:

header("Content-Type: application/javascript");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM