简体   繁体   中英

JQuery Undefined in IE8

At the bottom of my page, I am a jquery function that does NOT do an async call. It works fine in Chrome, FireFox. But in IE8 it says jQuery is undefined. What can be causing this?

<script src="/scripts/jquery" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        jQuery.ajax( "dev/js/templates", {
                async : false,
                success: function (data) {
                    $("#tpl_scripts").html(data);
                }

    })

    </script>

As a note, the script is being returned from PHP with header as

<?php 
$script = file_get_contents('/libs/scripts/jquery.1.8.2');
if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) {
            ini_set("zlib.output_compression", 1);
    }

    header('Content-type: text/javascript');

echo $script;
?>

This is going to be a long shot, but judging from your comments I have some idea what's going on.

You said that PHP concatenates multiple JavaScript files together and serves it in one go; this is generally a good idea (although I would go for static compilation and still use only the web server).

Internet Explorer is notorious for being pedantic about comma placement in objects, eg

var a = {
    x: 123,
    y: 456,
}

See that extraneous comma after the declaration of the y property? Most browsers don't care about that stuff and most PHP developers always use it for convenience.

Internet Explorer on the other hand bombs on this and stops running the rest of the code that follows. In your case, that could include jQuery if it was preceded by your own code (because John Resig would never make that "n00b" mistake :))

尝试将.php添加到脚本标记的src属性中

<script src="/scripts/jquery.php" type="text/javascript"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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