Jump to content

if statement strange outcome


jimfog
 Share

Recommended Posts

Take a look at this code here:function john(){if(window.getSelection){alert("hi")};}john();Normally it should output an alert box ONLY when there is a selection in the page.Nonetheless, it outputs the alert box even when nothing is selected.Is that normal?I did my tests in the firebug console.

Link to comment
Share on other sites

Take a look at this code here:function john(){if(window.getSelection){alert("hi")};}john();Normally it should output an alert box ONLY when there is a selection in the page.Nonetheless, it outputs the alert box even when nothing is selected.Is that normal?I did my tests in the firebug console.
Yes, that is normal. What you are actually testing for is the existence of the getSelection method, not whether there is a selection. If you add parens to getSelection, it should test whether there is a selection on the page:if (window.getSelection()) {
Link to comment
Share on other sites

Yes, that is normal. What you are actually testing for is the existence of the getSelection method, not whether there is a selection. If you add parens to getSelection, it should test whether there is a selection on the page:if (window.getSelection()) {
The problem continues, despite the revised code.Here is the revised code:function john(){if(window.getSelection()){alert("hi")};}john();Again, i get the alert box.What might be wrong?
Link to comment
Share on other sites

Yeah, window.getSelection() returns an object, not a string, so it always evaluates to Boolean true.
Not sure why, but I was thinking the toString method should be automatically called when testing it in the if statement. After thinking about it, I realized that isn't the case. It should only be called when the object is used as a string (for example, alert(window.getSelection()); )
Link to comment
Share on other sites

I'll say it again. Selections and text ranges are a PITA. Part of the problem is the way they can cross nodes. The DOM is really not set up to handle that, and the methods and properties that do exist are extremely weird.

Link to comment
Share on other sites

I'll say it again. Selections and text ranges are a PITA. Part of the problem is the way they can cross nodes. The DOM is really not set up to handle that, and the methods and properties that do exist are extremely weird.
Finally, the following code worked:function john(){if(window.getSelection()==0){alert("hi")};}john();I did not know the objects are evaluated to boolean values.One mistake i was doing is that instead "0" i tested again "false"(if(window.getSelection()==false)).I checked the tutorials and saw the mistake.Thanks.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...