外部链接的Javascript文件赢得't执行,但函数有效

Externally linked Javascript file won't execute, but the function works

本文关键字:执行 有效 函数 链接 Javascript 文件 外部      更新时间:2023-09-26

这很可能是一个非常简单的问题。

我有一个外部javascript文件链接到我的主html文件。链接很好,但函数在调用时不会运行。我知道这个函数是有效的,因为我可以将它复制并粘贴到我的主html文件中。但是,一旦它在外部文件中,该函数就不会运行。

我做错了什么?

我的主html文件的代码段:

<html>
<head>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="VRC_Index_Ajax_Functions.js"></script><--The issue file-->
    <script type="text/javascript" src="validations.js"></script> <--This file works-->

这是我的整个VRC_Index_Ajax_Functions.js文件。我主要处理的函数是showHint(str)。我要提到的是,showHint_l(str)在这个文件中也不起作用。我还不确定其他功能。

 //VRC_Index_Ajax_Function.js - ajax calls

//Publisher Hints - First Name
function showHint(str)
{
 if (str.length==0)
 {
   document.getElementById("txtHint").innerHTML="";
   return;
 }
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
 }
 }
 xmlhttp.open("GET","gethint.php?q="+str,true);
 xmlhttp.send();
}
//Publisher Hints - Last Name
function showHint_l(str)
{
 if (str.length==0)
{
 document.getElementById("txtHint").innerHTML="";
 return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
 xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint2.php?q="+str,true);
xmlhttp.send();
}
 //Ajax function for checking out territories - it will simply call the php file
 function checkOut(params)
{
var urlString = params;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
    document.getElementById("test").innerHTML=xmlhttp.responseText; 
}
//Setup for post submission 
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.open("POST","checkOut.php",true);
xmlhttp.send(urlString);
}

//Function that displays checked out territories
function displayChOut(params)
{
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("displayCO").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("POST","ajax_checked_out.php",true);
xmlhttp.send();
 }
 function checkStatus()
 {
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if(xmlhttp.readystate == 4 && xmlhttp.status == 200)
    {
    document.getElementsByName("numberOut").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "checkStatus_php.php", true);
xmlhttp.send();
 }

我真的不知道为什么它会突然在外部文件中不起作用。如有任何帮助,我们将不胜感激。

您错过了一个函数的右大括号:

function showHint(str) {
    if (str.length === 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gethint.php?q=" + str, true);
    xmlhttp.send();
}
//Publisher Hints - Last Name
function showHint_l(str) {
    if (str.length === 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gethint2.php?q=" + str, true);
    xmlhttp.send();
}
//Ajax function for checking out territories - it will simply call the php file
function checkOut(params) {
    var urlString = params;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("test").innerHTML = xmlhttp.responseText;
        }
        //Setup for post submission 
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.open("POST", "checkOut.php", true);
        xmlhttp.send(urlString);
    }
} // Here

//Function that displays checked out territories
function displayChOut(params) {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("displayCO").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "ajax_checked_out.php", true);
    xmlhttp.send();
}
function checkStatus() {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
            document.getElementsByName("numberOut").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "checkStatus_php.php", true);
    xmlhttp.send();
}

与您的问题无关,但有一点可维护性:

由于此代码,xmlhttp是全局的。

它已分配给,但未确定作用域。

要使变量成为函数范围的本地变量,请执行以下操作:

function somefunc() {
    var xmlhttp;
    ...
}

尝试将所有js-include移到html文件的末尾。也许您可以在DOM准备好之前调用此函数。

我还没有仔细查看代码,但如果javascript文件的一部分格式错误,那么整个文件将无法工作。如果您缺少大括号{}或括号,或者任何其他语法错误。