duanju9104 2013-07-03 12:38
浏览 30
已采纳

我正在尝试将包含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

  • 写回答

2条回答 默认 最新

  • doudeng2057 2013-07-03 12:40
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line