这很可能是一个非常简单的问题。
我有一个外部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();
}
我真的不知道为什么它会在外部文件中突然失效。任何帮助将不胜感激。
答案 0 :(得分:1)
你错过了一个函数的结束括号:
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;
...
}
答案 1 :(得分:0)
尝试将所有js include移动到html文件的末尾。 也许你在DOM准备好之前调用这个函数。
答案 2 :(得分:0)
我还没有查看代码,但是如果javascript文件的一部分格式错误,那么整个文件将无效。如果您缺少大括号{}或括号,或任何其他语法错误。