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 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到