如何将js代码完全从html文件中独立出来?

tree /var/www/html/ajax/
/var/www/html/ajax/
├── getuser.php
└── index.html

很简单的逻辑,index.html通过ajax,将数据发送到getuser.php,将数据库的查询结果传递到index.html里面。

已经调试成功。

index.html

<html>
    <head>
    </head>
    <body>
        <form> 
            Select a User:
            <select name="users" id="user">
                <option value="x1">x1</option>
                <option value="x2">x2</option>
                <option value="x3">x3</option>
            </select>
        </form>
        <input type="button" value="search" id="search">
        
        <p>
        <div id="txtHint"><b>User info will be listed here.</b></div>
        </p>
<script>
var xmlHttp
function showUser(){ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request")
        return
    }
    var mySelect = document.getElementById("user");
    var index = mySelect.selectedIndex;
    var str = mySelect.options[index].value;
    var url="getuser.php"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

function stateChanged(){ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
        document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
    } 
}

function GetXmlHttpObject(){
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }catch (e){
        //Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    } 
    return xmlHttp;
}

ob = document.getElementById("search");
ob.addEventListener("click",showUser);
</script>
    </body>
</html>

getuser.php

<?php
$q=$_GET["q"];
$dsn = "mysql:host=localhost;dbname=ajax";
$con = new PDO($dsn,"root","xxxx");
$query_check = "select * from person where `FirstName`='$q' ";
$result = $con->query($query_check);
$rows = $result->fetchAll();
print_r($rows);
?>

现在,我想将index.html里面的全部js代码,独立成一个js文件
形成下面的结构
tree /var/www/html/ajax/
/var/www/html/ajax/
├── getuser.php
├── index.html
└── selectuser.js

selectuser.js部分

var xmlHttp
function showUser(){ 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request")
        return
    }
    var mySelect = document.getElementById("user");
    var index = mySelect.selectedIndex;
    var str = mySelect.options[index].value;
    var url="getuser.php"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

function stateChanged(){ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
        document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
    } 
}

function GetXmlHttpObject(){
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }catch (e){
        //Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    } 
    return xmlHttp;
}

ob = document.getElementById("search");
ob.addEventListener("click",showUser);

就是将index.html里面的<script></script>之间的内容全部移到 selectuser.js
发现,这样做,无法正常运转。

请问,应当做哪些修改?

阅读 5.2k
3 个回答

分离出来,在html中引入这个js文件报错?

index.html 引入selectuser.js:

<script src="selectuser.js"></script>

你的这两个文件是在同一目录下的,所以直接写就可以。

@改名字很伤神 谁知道你的无法运转是什么意思,是文件加载不出来,还是加载出来js报错。
大概率是你引入js的位置不对,此时dom没有加载完。你要么重新检查引用位置,要么把执行代码用window.onload包裹起来。
他说中了。

写成这样
加了一个defer

<html>
    <head>
    <script src="selectuser.js" defer></script>
    </head>
    <body>
        <form> 
            Select a User:
            <select name="users" id="user">
                <option value="x1">x1</option>
                <option value="x2">x2</option>
                <option value="x3">x3</option>
            </select>
        </form>
        <input type="button" value="search" id="search">
        
        <p>
        <div id="txtHint"><b>User info will be listed here.</b></div>
        </p>
    </body>
</html>

或者

<html>
    <head>
    </head>
    <body>
        <form> 
            Select a User:
            <select name="users" id="user">
                <option value="x1">x1</option>
                <option value="x2">x2</option>
                <option value="x3">x3</option>
            </select>
        </form>
        <input type="button" value="search" id="search">
        
        <p>
        <div id="txtHint"><b>User info will be listed here.</b></div>
        </p>
    </body>
    <script src="selectuser.js"></script>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题