This is more than likely a very simple question.
I have an external javascript file linked to my main html file. The link is good, but the functions won’t run when called. I know for a fact that the function works because I can copy and paste it into my main html file. However, the instant it is in an external file, the function won’t run.
What am I doing wrong?
Snippet of my main html file:
<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-->
This is my entire VRC_Index_Ajax_Functions.js file. The function I’m dealing with mainly is showHint(str). I will mention that the showHint_l(str) doesn’t work within this file either. I’m not sure about the other functions yet.
//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();
}
I really have no idea why it would suddenly not work in an external file. Any help would be appreciated.
You missed a closing brace on a function:
Not quite related to your question but a point of maintainability:
xmlhttpis a global due to this code.It’s assigned to without being scoped.
To make a variable local to a function scope do this: