我正在尝试将包含XMHTTPRequest脚本的js文件src到我的HTML中

If I embed my XHR file into my HTML document directly, everything works fine. As soon as I src it via <script type="text/javascript" src="js/ajax_gallery.js">ajax_json_gallery('gallery');</script>

Nothing works, and I get no errors. I'm assuming it's something to do with the XHR being created in a separate file to the HTML. I just don't like XHR script cluttering up my HTML, I just want to load as an external JS file.

I've moved my main 3 scripts, galleryHandle.php, XHR.js, ajax_gallery.html all to the same dir level to keep things simple. And the gallery images are in a folder called "gallery", also on the same level.

Here's my code: HTML

<html>
<head>
    <title>Test</title>
    <link rel="stylesheet" type="text/css" href="css/gallery.css" />
</head>
<body>
    <div id="pagetop"></div>
    <div id="thumbnailbox"></div>
    <div id="pictureframe"></div>   
    <script type="text/javascript" src="XHR.js">ajax_json_gallery('gallery');</script>
</body>
</html>

JavaScript

function ajax_json_gallery(folder) {
        "use strict";
        var httpRequest = new XMLHttpRequest();
        document.getElementById("pagetop").innerHTML = "dynamic ajax json gallery";
        var thumbnailbox = document.getElementById("thumbnailbox");
        httpRequest.open("POST", "galleryHandle.php", true);
        httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState === 4 && httpRequest.status === 200) {
                var data = JSON.parse(httpRequest.responseText);
                var pictureframe;
                pictureframe.innerHTML = "<img src='"+data.img1.src+"'>";
                thumbnailbox.innerHTML = "";
                for (var obj in data) {
                    if (data[obj].src){
                        thumbnailbox.innerHTML += '<div onclick="putinframe(\''+data[obj].src+'\')"><img src="'+data[obj].src+'"></div>';
                    }
                }
            }
        };
        httpRequest.send("folder="+folder); 
        thumbnailbox.innerHTML = "Loading...";
}
    function putinframe(src) {
        "use strict";
        var pictureframe = document.getElementById("pictureframe");
        pictureframe.innerHTML = '<img src = " '+src+' " >';
        }

PHP

<?php
header("Content-Type: application/json");
//bring in folder name
$folder = $_POST["folder"];
//start componding json 
$jsonData = '{';
//compound directory path
$dir = $folder."/";
//open directory
$dirHandle = opendir($dir);
//init while looop 
$i = 0;
while ($file = readdir($dirHandle)) {
    if(!is_dir($file) && strpos($file, '.jpg')){
        $i++;
        $src = "$dir$file";
$jsonData .= '"img'.$i.'":{ "num":"'.$i.'","src":"'.$src.'", "name":"'.$file.'" },';
    }
}
closedir($dirHandle);
$jsonData = chop($jsonData, ",");
$jsonData .= '}';
echo $jsonData;
?>

I understand there are some redundancies in my code but it's just a tutorial I'm going through to learn the basics of JSON building with POST, XHR.

Anyway, help appreciated as always. Thanks

Explanation

FROM W3C:

<script type="text/javascript" src="myscript.js">
  alert('I am pointless as I won\'t be executed');
</script>

Upon meeting this element in a page, browsers will then load the file myscript.js and execute it. Any content inside the script element itself will be skipped when you provide a src attribute. The [last] example will load the file myscript.js and execute the code in it but will not execute the alert inside the script element at all.


Solution

Try the following in your head tags:

HTML

<script type="text/javascript" src="XHR.js"></script>
<script type="text/javascript">
    ajax_json_gallery('gallery');
</script>

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

You can't have src attribute and javascript both in a single tag. Separate them out. Like this...

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

<script type="text/javascript">ajax_json_gallery('gallery');</script>