SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Weird Js problem

    Hey,

    I'm facing a pretty weird problem and I havn't figured out how to solve it yet.

    I've got a public var in my script and a js-class which modifies it.

    Code:
    var publicVar = null;
    
    function jsClass()
    {
      this.method = function()
      {
        publicVar = true;
      }
    }
    I now call the class via a function

    Code:
    function name()
    {
      var obj = new jsClass();
    
      // let's call the method
      obj.method();
    
      // now output the global var
      alert(publicVar);
    }

    The name()-function is called via onClick="return name();" on a button.

    Well, it should now return true...but it returns null...BUT if I now click on the button a second time, it returns TRUE!!
    I dunno what it is that stuffs up

    I then changed the code into this...just playing around :
    // now output the global var
    alert(publicVar);
    alert(publicVar);

    What happens is...the first alert shows null...the second alert shows "true"!

    Why can't it return true the first time??

    To explain things:
    -------------------
    I'm using an AJAX class + a function which calls this class, as seen in the script above...might the AJAX might influence timing??

    Can anybody help me?

    I appreciate your help!

    Cheers, Florian
    Last edited by ultimazer1; Mar 17, 2006 at 07:16.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is that really the code you've been using? I see two typos that would probably keep it from working.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    no it's a "model" code...a simple abbreviation of the functions/classes i'm using
    To put all the code here would definitely take up too much space.

    I fixed the typos as well

    Cheers, Florian

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above code works fine. So whatever is causing this is not in there.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alrighty,
    this is the code which ís called by OnSubmit:

    "httpSocketBoolean" is the global variable which is modified by the ajax class.

    Code:
    function validateForm(formId, mode)
    {
    	// get form information
    	var form       = document.getElementById(formId);
    	var formAction = form.getAttribute('action');
    
    	if(typeof mode == 'undefined')
    	{
    		// build ajax call
    		var SendForm = new httpSocket();
    		SendForm.setRequest(formAction);
    		SendForm.setFormId(formId);
    		
    		SendForm.process();
    		validateFormTimeout = window.setTimeout("validateForm('"+formId+"','yes')", 100);
    		return false;
    	}
    	else
    	{
    		if(typeof validateFormTimeout != 'undefined')
    		{
    			window.clearTimeout(validateFormTimeout);
    			if(httpSocketBoolean == true)
    			{
    				form.submit();
    			}
    		}
    	}
    }
    This code works. But as you can see I put a setTimeout in there...this seemingly makes things work and fixes the suspected timingproblem. A timeout below 100 msecs still leads to same problem as before though...so time must be a factor.

    Doing things this way the function will only return FALSE though. Of course the function calls itself after that, but returning TRUE in this second call seemingly is not returned to the form so nothing happens. I thus had to explicitly submit(); the form.

    A negative effect of this is that once u submit the form and then click back in your browser all input in the form is gone. Normally the Browser will display what u typed in before.

    In order to fix this:
    This is how it should work theoretically...but the "httpSocketBoolean" variable isn't changed yet when it's RETURNed...that's the timing problem

    Code:
    function validateForm(formId)
    {
    	// get form information
    	var form       = document.getElementById(formId);
    	var formAction = form.getAttribute('action');
    
    	// build ajax call
    	var SendForm = new httpSocket();
    	SendForm.setRequest(formAction);
    	SendForm.setFormId(formId);
    		
    	SendForm.process();
    	return httpSocketBoolean;
    }
    Have you guys an idea how to fix it?

    Thanks for your help

    Cheers, Florian


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •