简体   繁体   English

PHP生成的JavaScript文件不执行

[英]php generated javascript files don't excecute

I'm trying to create a module for Drupal to clean up some of the inline javascript that's put on every page. 我正在尝试为Drupal创建一个模块,以清理放在每个页面上的一些内联JavaScript。 The problem is, the files that I am generating are not being executed by the browser when they are included with the <script> tag. 问题是,当我生成的文件包含在<script>标记中时,它们不会被浏览器执行。 Here's a basic overview of what I'm doing: 这是我正在做的基本概述:

Module specific code (this code is executed whenever mysite.com/jqsc/MD5_HASH_GOES_HERE is visited, passing the MD5 sum as the first parameter): 特定于模块的代码(只要访问mysite.com/jqsc/MD5_HASH_GOES_HERE,并且将MD5和作为第一个参数传递,就会执行此代码):

function jquery_settings_cache_get($hash) {
    header('content-type: application/x-javascript');

    if (file_exists(dirname(__FILE__) . "/cache/{$hash}")) {
        exit(file_get_contents(dirname(__FILE__) . "/cache/{$hash}"));
    }
    else {
        watchdog('jqsc', "missing file {$hash}", null, WATCHDOG_ERROR);
        exit("// JQuery Settings Cache error -- missing file {$hash}");
    }
}

Current HASH.js file (this is linked from a script tag in the HTML template): 当前的HASH.js文件(这是从HTML模板中的脚本标签链接的):

alert('it works!');

Current HTML output (some irrelevant code has been removed): 当前的HTML输出(一些无关的代码已被删除):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <base href="http://example.com/sites/all/themes/mytheme/" />
  <script type="text/javascript" src="/sites/default/files/js/js_80a1e7d5d876d5176617603a4bbe39fe.js"></script> <!-- This file loads fine -->
  <script type="text/javascipt" src="/jqsc/deaa654d439f644fd473db0af4d993fe.js"></script> <!-- This file never loads -->
</head>
<body>
</body>
</html>

Navigating to mysite.com/jqsc/HASH.js brings up the file with the correct contents (just an alert() for the moment). 导航到mysite.com/jqsc/HASH.js会显示具有正确内容的文件(当前仅是alert())。 There are no comments to be unclosed, missing function declarations, or anything else a google search for "external javascript not executing" yielded. 没有注释需要关闭,缺少函数声明或其他任何Google搜索“外部JavaScript无法执行”的内容。 Firebug shows that the 2nd file is never executed (it never stops at a breakpoint set within the file). Firebug显示第二个文件永远不会执行(它永远不会在文件中设置的断点处停止)。 Live HTTP Headers shows that the correct content-type has been set (I've tried application/javascript, application/x-javascript, and text/javascript all with the same effect. Putting file #2 before file #1 has no effect. Changing the transport-encoding has no effect. Changing the charset has no effect. Changing the <script> tag to use language=javascript with or without type="text/javascript" or omitting all <script> tag properties except src has no effect. 实时HTTP标头显示已经设置了正确的内容类型(我尝试使用application / javascript,application / x-javascript和text / javascript都具有相同的效果。将文件#2放在文件#1之前无效。更改传输编码无效。更改字符集无效。更改<script>标记以使用使用language=javascript且带有或不带有type="text/javascript"或忽略除src之外的所有<script>标记属性均无效。 。

I'm officially stuck. 我被正式困住了。 Anything you can think of to dig me out of this hole is greatly appreciated. 非常感谢您能想到的将我从这个漏洞中挖出来的任何方法。

You might be calling the wrong file here: 您可能在这里调用了错误的文件:

<script type="text/javascipt" src="/jqsc/deaa654d439f644fd473db0af4d993fe.js"></script>

on second thought. 再三考虑。 Disregard that because of utter failishness. 由于完全失败而无视它。

The "there is a typo in your javascript" comment above is much closer to the truth probably ;) 上面的“您的javascript中有错字”注释可能更接近事实;)

<script type="text/javascript" src="/jqsc/deaa654d439f644fd473db0af4d993fe.js"></script>

:D :D

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

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