jimfog Posted July 12, 2011 Share Posted July 12, 2011 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 More sharing options...
ShadowMage Posted July 12, 2011 Share Posted July 12, 2011 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 More sharing options...
jimfog Posted July 12, 2011 Author Share Posted July 12, 2011 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 More sharing options...
ShadowMage Posted July 12, 2011 Share Posted July 12, 2011 Perhaps try converting to a string and testing against an empty string?if (window.getSelection().toString() == '') { Link to comment Share on other sites More sharing options...
jeffman Posted July 12, 2011 Share Posted July 12, 2011 Yeah, window.getSelection() returns an object, not a string, so it always evaluates to Boolean true. Link to comment Share on other sites More sharing options...
thescientist Posted July 12, 2011 Share Posted July 12, 2011 always know your enemyhttps://developer.mozilla.org/en/DOM/window.getSelection Link to comment Share on other sites More sharing options...
ShadowMage Posted July 12, 2011 Share Posted July 12, 2011 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 More sharing options...
jeffman Posted July 12, 2011 Share Posted July 12, 2011 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 More sharing options...
jimfog Posted July 13, 2011 Author Share Posted July 13, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.