Function works and then says not defined

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2008
Posts: 255
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 20
samarudge samarudge is offline Offline
Posting Whiz in Training

Function works and then says not defined

 
0
  #1
Nov 3rd, 2009
Hi,
I have some code to dynamically retrieve one of three AJAX files and insert it into a DIV

The Javascript is
  1. function createajax() {
  2. var Ajax;
  3. try{
  4. Ajax = new XMLHttpRequest();
  5. } catch (e){
  6. try{
  7. Ajax = new ActiveXObject("Msxml2.XMLHTTP");
  8. } catch (e) {
  9. try{
  10. Ajax = new ActiveXObject("Microsoft.XMLHTTP");
  11. } catch (e){
  12. alert("Error!!!");
  13. return false;
  14. }
  15. }
  16. }
  17. return Ajax;
  18. }
  19.  
  20. function basicAjaxSwitch(File, Recipient, Animate) {
  21. if ( Animate == true ) {
  22. Animation(document.getElementById(Recipient)).duration(100).from('opacity', 1).to('opacity', 0).go();
  23. }
  24. basicAjaxSwitch = createajax();
  25. basicAjaxSwitch.onreadystatechange = function(){
  26. if(basicAjaxSwitch.readyState == 4){
  27. if ( Animate == true ) {
  28. Animation(document.getElementById(Recipient)).duration(100).checkpoint().duration(100).from('opacity', 0).to('opacity', 1).go();
  29. setTimeout('document.getElementById("'+Recipient+'").innerHTML = basicAjaxSwitch.responseText;', 100);
  30. } else {
  31. document.getElementById(Recipient).innerHTML = basicAjaxSwitch.responseText;
  32. }
  33. }
  34. }
  35. basicAjaxSwitch.open("GET", "/ajax/"+File, true);
  36. basicAjaxSwitch.send(null);
  37. }
  38.  
  39. function changesidebarinfo(changeto) {
  40. if ( changeto == 'login' ) {
  41. Animation(document.getElementById('main_layout_accountlinks_background')).to('left', '0px').duration(100).go();
  42. Animation(document.getElementById('main_layout_accountlinks_login')).duration(100).to('color', '#FFF').go();
  43. Animation(document.getElementById('main_layout_accountlinks_signup')).duration(100).to('color', '#000').go();
  44. Animation(document.getElementById('main_layout_accountlinks_about')).duration(100).to('color', '#000').go();
  45. basicAjaxSwitch('contentinfo/login.php', 'main_layout_rsbinfo_container', true);
  46. } else if ( changeto == 'join' ) {
  47. Animation(document.getElementById('main_layout_accountlinks_background')).to('left', '50px').duration(100).go();
  48. Animation(document.getElementById('main_layout_accountlinks_login')).duration(100).to('color', '#000').go();
  49. Animation(document.getElementById('main_layout_accountlinks_signup')).duration(100).to('color', '#FFF').go();
  50. Animation(document.getElementById('main_layout_accountlinks_about')).duration(100).to('color', '#000').go();
  51. basicAjaxSwitch('contentinfo/join.php', 'main_layout_rsbinfo_container', true);
  52. } else if ( changeto == 'about' ) {
  53. Animation(document.getElementById('main_layout_accountlinks_background')).to('left', '100px').duration(100).go();
  54. Animation(document.getElementById('main_layout_accountlinks_login')).duration(100).to('color', '#000').go();
  55. Animation(document.getElementById('main_layout_accountlinks_signup')).duration(100).to('color', '#000').go();
  56. Animation(document.getElementById('main_layout_accountlinks_about')).duration(100).to('color', '#FFF').go();
  57. basicAjaxSwitch('contentinfo/about.php', 'main_layout_rsbinfo_container', true);
  58. }
  59. }

And the HTML that triggers the actions is
  1. <div id="main_layout_accountlinks">
  2. <div id="main_layout_accountlinks_background">&nbsp;</div>
  3. <div id="main_layout_accountlinks_login" onclick="changesidebarinfo('login');">Log In</div>
  4. <div id="main_layout_accountlinks_signup" onclick="changesidebarinfo('join');">Join</div>
  5. <div id="main_layout_accountlinks_about" onclick="changesidebarinfo('about');">About</div>
  6. </div>

When I click one of the 3 buttons (doesn't matter which) the result is exactly as intended. Then when I click another (or the same) button, nothing happens and Firebug gives the error
  1. basicAjaxSwitch is not a function
  2. changesidebarinfo("login")site.js (line 61)
  3. function onclick(event) { changesidebarinfo("login"); }(click clientX=816, clientY=41)2 (line 2)
  4. [Break on this error] basicAjaxSwitch('contentinfo/login.php...'main_layout_rsbinfo_container', true);\n

Regards,
Sam Rudge
Last edited by samarudge; Nov 3rd, 2009 at 11:44 am.
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,407
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
1
  #2
Nov 3rd, 2009
Inside the function basicAjaxSwitch you are defining a variable basicAjaxSwitch so it's being overwritten. Just use a different variable name inside the function.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC