Results 1 to 6 of 6

Thread: [RESOLVED] Another JavaScript issue, code in if block

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Resolved [RESOLVED] Another JavaScript issue, code in if block

    I have the bit of code below that is not behaving as I expected.
    Code:
    function StatusSet(req){
    		if (req.status == 200) {	
    			$("#scrmsg").html("");
    			var arr = new Array();
    			var response = req.responseText;
    			arr = response.split("~|~");
    			if (arr[0]==0){			
    				window.location.reload();
    			}else{
    				$("#scrmsg").html(arr[1]);
    				return;	
    			}			
    		}else{
    			$("#scrmsg").html('Call to server failed. Error='+req.status);
    		}		
    	}
    I am sending the data back so that arr[0] is not = 0 and arr[1] contains a message that should be displayed on screen.
    If I comment out the window.location.reload(); line then I see the message as expected.
    If that line uncommented then the page reloads and the message is not seen.

    Any idea why it is executing the page reload when arr[0]==0 is false?
    Last edited by DataMiser; Jan 27th, 2022 at 08:10 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Another JavaScript issue, code in if block

    What do you see if you put

    Code:
    alert(arr[0]);
    in place of window.location.reload()?

    What happens if you use

    Code:
    if (arr[0]===0 ){
    in place of if (arr[0]==0 ){?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Another JavaScript issue, code in if block

    Interesting. It appears that code is executing more than once. In some cases anyway. I'll have to dig deeper, maybe something else is going on.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Another JavaScript issue, code in if block

    hmm very odd. I simplified the code a bit as a test. Got rid of the array and just used a plain string which contains either "OK" or some message to be displayed.
    Using basically the same block of code as posted it seems to work as expected.

    When using the previous code and placing an alert box in there I would see that block execute once showing arr[0] as empty then it would execute again and show it with a value.
    The expectation was that it should execute only once and always have a value then behave according to the value of said value but it appears there may be some eels in that hover craft

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Another JavaScript issue, code in if block

    Well I'm still not sure why it was not working with the array and the 0 in the first element but as it turns out using the string should be enough in this case so I will mark this one resolved and move on to the next potential problem.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Another JavaScript issue, code in if block

    Update: It appears that the root of the problem was me failing to check the ready state. The function was firing before the array was populated and then firing again after it was populated. Still weird that it executed the code inside the conditional when the array element was empty but it seems to work fine after adding the test of the ready state in there.

Posting Permissions

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



Click Here to Expand Forum to Full Width