/    Sign up×
Community /Pin to ProfileBookmark

if else block is not working logically

Hi,
The following code fragment is bothering me way too much because it is running in a nonsensical fashion:

[CODE]
function popup(url, name, offset, modal) {

for(i = 0; i < pl; i++) {
if(pw[i].closed)
continue;
if(pw[i].name.equals(name))
break;
}

if(i == pl) {
pw[i] = window.open(url, name, “toolbar=no, menubar=no, copyhistory=no, location=no, scrollbars=yes”
+ “, screenX=” + x + “, screenY=” + y
+ “, top=” + y + “, left=” + x
+ “, width=” + width + “, height=” + height);
alert (“i: “+i+”npl: “+pl+”ni == pl ? “+(i == pl));
pl++;
}
else if(pw[i].closed) {
pw[i] = window.open(url, name, “toolbar=no, menubar=no, copyhistory=no, location=no, scrollbars=yes”
+ “, screenX=” + x + “, screenY=” + y
+ “, top=” + y + “, left=” + x
+ “, width=” + width + “, height=” + height);
}
}
[/CODE]

As you have guessed this is the code for generating popup. Problem happens only with firefox and when a user double clicks the link corresponding to the popup.

The alert that is generated is completely weird. Double click causes to open 2 popups. For the first popup the output is shown in aberration.jpg. The output is ok. For the second popup the output is shown in aberration1.jpg. Here lies the problem. As you see from the code the alert is only shown when entering the first branch:

[CODE]if (i == pl) [/CODE]

so according to common sense when i and pl are equal then the execution would enter the if block and print the alert. But the output is showing that i = 0 and pl = 1 and i == poplen ? false.

How is this possible? if i != pl how can

[CODE]if (i == pl) [/CODE]

return true and the enter into the block?

Please help here. wasting too much time here.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@hoangkcSep 17.2007 — I try to understand your code:

what is pl? I guess it is [I][B]length[/B][/I] of pw
for(i = 0; i &lt; pl; i++)
{
#1 if(pw[i].closed) continue;
#2 if(pw[i].name.equals(name)) break;
}
if(i == pl)
{
#group1
}
else if(pw[i].closed)
{
#group2
}

As I know, there are 2 ways to escape for-loop. The 1st is i<pl (and pw[i] has name attribute equals [I]name[/I](parameter) and pw[i].closed == false)

The 2nd is i == pl.

Therefore, the code never run into #group2.
Copy linkTweet thisAlerts:
@amishera2007authorSep 17.2007 — pl is indeed the length of the array. it is used because the array might be of any length but there might be content upto elements at pl-1.
Copy linkTweet thisAlerts:
@amishera2007authorSep 18.2007 — Hello hoangkc,

Thanks for your reply.

The question is not whether the execution should enter the second block or not. The question is when (i != pl) then it should not enter block 1. In that case according to your claim, first if test should fail, then 2nd if test should fail too and as there are no other if tests the execution should finish. I mean it should enter neither block 1 nor block 2. But here we see that even if the first test fails the execution enter the first block (how??) which can be verified from the images provided.

Moreover, second if test can be true as pw contains the opened windows. If the i-th window is closed by the user then second if test would succeed.

The conclusion is that, as evident from the output images we see that even if (i != poplen) , it enters into a block that is previously tested by the condition if (i == poplen). So how could an execution fail an if test but enter its corresponding block? That remains a mystery.

Here are the attachments:

[ATTACH]9605[/ATTACH]

[ATTACH]9606[/ATTACH]

[upl-file uuid=0af7f405-741a-4977-b379-d2a20668f168 size=16kB]aberration.JPG[/upl-file]

[upl-file uuid=8fade679-ee5f-481d-8218-a4e12fd520bb size=16kB]aberration1.JPG[/upl-file]
Copy linkTweet thisAlerts:
@hoangkcSep 18.2007 — hi, amishera2007.

I saw your attachments. And I copy and try your code but I didn't meet the situation that you met.

I also want to know how it can go #group1 if i!=pl (as you said). So can you show more your code, or can you have the link to try it on www?
Copy linkTweet thisAlerts:
@amishera2007authorSep 19.2007 — Hi,

I can not reveal any more code. But I guess I figured out the problem though I could not find the solution.

Here is the thing. the [B][I]pl[/I][/B] and [B][I]pw[/I][/B] both are global variables. Now for firefox double clicking means creating two threads (I dont how 2 threads are generated). I did another thing to understand the problem better. I kept the value of[B][I] pl [/I][/B]to a local variable [B][I]localpl[/I][/B] just before entering the if block. Now the output shown is as follows:
[CODE]
for 1st popup:
i = 0
pl = 0
localpl = 0[/CODE]


[CODE]for 2nd popup:
i = 0
pl = 1
localpl = 0[/CODE]


Which means, before the 2nd popup could enter if block the value of [CODE]pl = 0.[/CODE] But by the time execution reaches the alert inside the block its value is changed by the 1st popup to 1. This is obviously a race condition. Now the question is how to resolve this concurrency problem using pure javascript without resorting to any ajax? If there were any thing like synchronized block as in java then the problem would be solved ie I need the global variable [B][I]pl[/I][/B] and [B][I]pw[/I][/B] to be locked when writing values.

There could be another solution to uproot this problem. Because the main culprit is firefox and double click, the problem can be solved by disabling double click by changing minimal code. All I need is write a ondblclick event in the body tag and when this event is fired there should be a check whether that particular link is pressed or not. Then convert the 2 requests into 1 request.

Thanks in advance.
Copy linkTweet thisAlerts:
@KorSep 19.2007 — what about if using a local variable as indent, not a global...

for([COLOR="Blue"][B]var[/B][/COLOR] i=0.....)
Copy linkTweet thisAlerts:
@WebnerdSep 19.2007 — There is also the issue of pw[i].closed when pw[i] may not even be defined yet. so, you can't access the .closed property of pw[i] if pw[i] does not exist.



<i>
</i>else if(!pw[i] || (pw[i] &amp;&amp; pw[i].closed))
Copy linkTweet thisAlerts:
@hoangkcSep 19.2007 — amishera2007,

can you try your original code, but instead using [I]alert ("i: "+i+"npl: "+pl+"ni == pl ? "+(i == pl));[/I], you should write it as text (maybe [I]var s = "i: "+i+"npl: ... "; document.getElementById("log").innerHTML += s + "<br/>"[/I]);

I don't think double-click will generate 2 thread, but alert.

However, if you can do that experiment, please let me know the result. I want to understand it and affirm my guess if it is right.
×

Success!

Help @amishera2007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...