Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2007, 4:37 PM   #1
FireflyX
PHP God
 
FireflyX's Avatar
 
Join Date: Nov 2007
Location: Stockton, England
Posts: 15
Rep Power: 0 FireflyX is on a distinguished road
Send a message via MSN to FireflyX
Question 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
FireflyX is offline   Reply With Quote
Old Nov 30th, 2007, 5:00 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Problem with calling a function

Try replacing the call to initialize() with window.onload = initialize;.
titaniumdecoy is offline   Reply With Quote
Old Nov 30th, 2007, 5:27 PM   #3
FireflyX
PHP God
 
FireflyX's Avatar
 
Join Date: Nov 2007
Location: Stockton, England
Posts: 15
Rep Power: 0 FireflyX is on a distinguished road
Send a message via MSN to FireflyX
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
FireflyX is offline   Reply With Quote
Old Dec 1st, 2007, 7:16 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Dec 1st, 2007, 7:41 PM   #5
FireflyX
PHP God
 
FireflyX's Avatar
 
Join Date: Nov 2007
Location: Stockton, England
Posts: 15
Rep Power: 0 FireflyX is on a distinguished road
Send a message via MSN to FireflyX
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
FireflyX is offline   Reply With Quote
Old Dec 1st, 2007, 7:53 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Problem with calling a function

Sane is correct...O great PHP god
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.
javascript Syntax (Toggle Plain Text)
  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. });
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270

Last edited by Dameon; Dec 1st, 2007 at 8:15 PM.
Dameon is offline   Reply With Quote
Old Dec 1st, 2007, 9:04 PM   #7
FireflyX
PHP God
 
FireflyX's Avatar
 
Join Date: Nov 2007
Location: Stockton, England
Posts: 15
Rep Power: 0 FireflyX is on a distinguished road
Send a message via MSN to FireflyX
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.
FireflyX is offline   Reply With Quote
Old Dec 2nd, 2007, 7:16 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Dec 5th, 2007, 4:58 PM   #9
FireflyX
PHP God
 
FireflyX's Avatar
 
Join Date: Nov 2007
Location: Stockton, England
Posts: 15
Rep Power: 0 FireflyX is on a distinguished road
Send a message via MSN to FireflyX
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
__________________
mysql_query("DELETE chavs FROM earth") or die("chavs");
FireflyX is offline   Reply With Quote
Old Dec 5th, 2007, 6:08 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling an undeclared or undefined function in C/C++ climbnorth C 2 Oct 8th, 2006 12:02 AM
problem with a function brad sue Other Programming Languages 3 Apr 23rd, 2006 10:24 PM
problem with standard function sparda C 8 Oct 26th, 2005 2:27 PM
Problem passing multi-dimensional array to function aznluvsmc C 2 Aug 30th, 2005 10:00 PM
Problem when returning a string from a function in VC++ vjkancha C++ 10 Mar 7th, 2005 1:18 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:41 PM.

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