Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird problem with AJAX and some javascript

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
I have a page with the following javascript:
function checkAll()
{
var myForm = document.getElementById('sCampaign');

for (var i=0; i<myForm.length; i++)
{
if ( myForm.elements.checked == true)
{
myForm.elements.checked = false;
}
else
{
myForm.elements.checked = true;
}
}
}

which checks/unchecks all the checkboxes on the form.

I make a call to this page via AJAX. When the form loads, I get an error saying "checkAll() is not defined". When I hit the page in different browser, everything works fine.

Does anyone know what goes wrong here?
thanks in advance

 
checkAll() is not defined means exactly that. You function is undefined. I think we would need more information to help you out, or we could throw out random guesses at what the problem may be. The best one I think would be you called the function it before it is fully defined, try moving your script to the head of the page.
 
this is how i call the page

var xmlHttp;
function displayInfo(url)
{
//alert(url);
setAppStatus('<font class="pagescopy" face="arial, helvetica, sans-serif" size="1" color="##666666">Loading... Please Wait</font>');

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}

url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("appMessage").innerHTML = xmlHttp.responseText;
}
else
{
document.getElementById("appMessage").innerHTML='<font class="pagescopy" face="arial, helvetica, sans-serif" size="1" color="##666666">Loading... Please Wait! </font><img src="/admin/rmd/images/spinner.gif" align="absmiddle" /> ';
}
}


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;
}






 
hmm.. what is the actual purpose with this script?
is hte AJAX requesting a JavaScript file??

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
First you choose a campaign, and it brings out a summary of bunch of things. User, then, does some sort of data drill down. I could have submit to next page, but I thought this would look much better.

 
OK!
But I don't know how/when/where the [tt][maroon]checkAll[/maroon][/tt] function is called and what is the AJAX requesting?? A HTML page, JSON, XML, JavaScript or just plain text??
And you should check the page [tt][maroon]status[/maroon][/tt] also, not only the [tt][maroon]readyState[/maroon][/tt]. The [tt][maroon]readyState[/maroon][/tt] only checks that the request is finished not OK. The [tt][maroon]status[/maroon][/tt] checked if the page is found (200 = OK, 404 = Not fould) etc.


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Kind of a nebulous description... here are some common issues that might help and a question in case they don't.

1. An XMLHTTPRequest to 'ajaxScript.php&sid=73467845' won't work. If you pass displayInfo a URL that doesn't already end in ?something it will form a bad URL by using & instead of ?.

This code checks to see if there is a ? already in the URL. If there is, it uses an &, if not, it uses a ?. This would be safer:
Code:
url=url + ((url.indexOf('?') < 0) ? '?' : '&') + "sid="+Math.random();

2. Browsers differ on URL handling for these requests. Making a request to 'ajaxScript.php' may not be the same as a call to '/ajaxScript.php' depending on your browser (use '/ajaxScript.php' with the leading slash).


And an important question:

Why is checkAll being called when the form loads? It seems like something that would be in a button's onclick handler. If you want the boxes to be checked automatically just add [tt]checked[/tt] in their tag ([tt]<input type="checkbox" name="manyboxes" value="choice2 checked>[/tt])
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top