Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Problem with calling a function (http://www.programmingforums.org/showthread.php?t=14648)

FireflyX Nov 30th, 2007 4:37 PM

Problem with calling a function
 
Ok just a quick question.. after sitting here for about half an hour trying to work out what's wrong with this script i come here in seek of guidence.

:

<script type="text/javascript">

function initialize(){

  var xmlHttp;
  try{
    xmlHttp = new XMLHttpRequest();   
  }catch (e){
    try{   
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");     
    }catch (e){     
      try{       
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");       
        }catch (e){
          alert("No ajax!");       
          return false;       
        }     
      }   
    } 
  }

function mail(){

initialize();

xmlHttp.onreadystatechange=function()
  {
  if(xmlHttp.readyState == 4)
    {
    document.getElementById("show_box").innerHTML = xmlHttp.responseText;
    }
  }

xmlHttp.open("GET","http://www.staticfire.co.uk/ajaxrequest.php?cmd=mail",true);
xmlHttp.send(null);

}

</script>


The problem seems to be that it isn't recognising the initialize() function. But if i replace where i call the function with the code for that function it all runs fine. Am i missing something obvious here?

-Thanks, Firefly

titaniumdecoy Nov 30th, 2007 5:00 PM

Re: Problem with calling a function
 
Try replacing the call to initialize() with window.onload = initialize;.

FireflyX Nov 30th, 2007 5:27 PM

Re: Problem with calling a function
 
Thanks for the reply but that didn't seem to make a difference. I can fix the problem by just putting the code for that function in each time i wish to use it but that seems very inconvinient :(

Sane Dec 1st, 2007 7:16 PM

Re: Problem with calling a function
 
Try moving var xmlHttp; outside the scope of the function. Make it the first line of the code, and not within the function call. I think that's because mail() refers to xmlHttp, but xmlHttp expires once initialize() ends.

FireflyX Dec 1st, 2007 7:41 PM

Re: Problem with calling a function
 
you know what.. that just worked! Thanks for the help my site now has a pm system with no page reloads :D

Dameon Dec 1st, 2007 7:53 PM

Re: Problem with calling a function
 
Sane is correct...O great PHP god :icon_rolleyes:
I'd suggest the Firebug extension for Firefox (includes javascript debugging, a javascript command line, and can show ajax requests real time, among other things). This would have told you that initialize() was actually getting called.

It would also be much less painful to use a library such as prototype or jQuery

jQuery equivalent looks something like this.
Warning: Untested.
:

  1. $('#myAwesomeButtonID').click(
  2.         function() {
  3.                 $('#show_box').load('/kickass_script.php',
  4.                       { mysteriousParameter: 42 });
  5.                 //Prevent the button from triggering form submission
  6.                 return false;
  7.         });


FireflyX Dec 1st, 2007 9:04 PM

Re: Problem with calling a function
 
Quote:

Sane is correct...O great PHP god
heh, i just wish i was the god of ajax right now

Perhaps i should look at getting this Firebug extension you speak of as it sounds like it could be useful.

Sane Dec 2nd, 2007 7:16 AM

Re: Problem with calling a function
 
Yep. I use that (although I didn't have to in this case). Mine's called the Web Developer extention, very handy. But Dameon could be referring to something similar.

FireflyX Dec 5th, 2007 4:58 PM

Re: Problem with calling a function
 
Hi again sorry to bump this thread back up but i thought this was working fine till a friend told me he was getting errors from it. I tested the system on a few browsers and it was fine on IE7, Opera and Safari but on Firefox and Netscape it just comes up with "Length required". Can anyone tell me why this might happen? Thanks

The Dark Dec 5th, 2007 6:08 PM

Re: Problem with calling a function
 
You should mention the actual URL so we could look at it not working, or if it is a private page, post your current code. Also, the line number of the error would be a help.


All times are GMT -5. The time now is 1:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC