-
Jan 27th, 2022, 08:06 PM
#1
[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.
-
Jan 27th, 2022, 09:00 PM
#2
Re: Another JavaScript issue, code in if block
What do you see if you put
in place of window.location.reload()?
What happens if you use
in place of if (arr[0]==0 ){?
-
Jan 27th, 2022, 09:39 PM
#3
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.
-
Jan 27th, 2022, 09:53 PM
#4
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
-
Jan 28th, 2022, 08:53 AM
#5
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.
-
Jan 30th, 2022, 10:49 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|