We help IT Professionals succeed at work.

What is the purpose of this line of javascript...

SAbboushi
SAbboushi asked
on
In the script below, what is the purpose of
return d.selection ?

Open in new window


Is there ever a case where it will be other than "undefined"?


button onclick="SearchSite()">Search Site</button>

function SearchSite() {
    javascript: 
    dd = document.domain;
    s = se(document);
    for (i = 0; 
    i < frames.length && !s; 
    i++
    )
        s = se(frames[i].document);
    if (!s || s == '')
        s = prompt('Google search ' + dd + ' for:', '');
    location.href = ('http://www.google.com/search?as_q=' + s + '&as_sitesearch=' + dd);
    function se(d) {
        return d.selection ? 
        d.selection.createRange().text 
        : 
        d.getSelection()
    }
}

Open in new window

Comment
Watch Question

AdamSenior Developer

Commented:
The whole line is:

       return d.selection ?
        d.selection.createRange().text
        :
        d.getSelection()

These expressions have the form:

(expression) ? "value if true" : "value if false";

So your expression, if d.selection has a value (i.e. it equals true), will return d.selection.createRange().text or, if not, then it will return whatever the function d.getSelection() returns

Author

Commented:
Thanks!

In this script, I believe that d.selection will never have a value (correct?).  

In other words, is there ever a case where
d.selection.createRange().text 

Open in new window

is executed?

I am finding that its value is always "undefined", so I don't believe
d.selection.createRange().text 

Open in new window

 will ever be executed in which case, what is the purpose for testing whether it has a value or not?
CERTIFIED EXPERT

Commented:
It depends what you send to the se() function.
The control you send to the function if it has a selection (selcted) it will return it!

Author

Commented:
>> It depends what you send to the se() function.  The control you send to the function if it has a selection (selcted) it will return it!

So can you give me an example of what control I could select that would result in a value being assigned to d.selection?
Software Engineer
CERTIFIED EXPERT
Commented:
It's brower dependent so if you use FireFox "d.selection" is always undefined.

The se() function is designed to return d.getSelection() on browers that don't support d.selection (which is only supported in Internet Explorer).
Robert SchuttSoftware Engineer
CERTIFIED EXPERT

Commented:
I modified the code slightly to show an alert including the information which part of the se() function was executed.
function SearchSite() {
    javascript: 
    dd = document.domain;
    s = se(document);
    for (i = 0; i < frames.length && !s; i++)
        s = se(frames[i].document);
    if (!s || s == '')
        s = prompt('Google search ' + dd + ' for:', '');
    //location.href = ('http://www.google.com/search?as_q=' + s + '&as_sitesearch=' + dd);
    alert('selected text: "' + s + '" (domain: "' + dd + '")');
    function se(d) {
        return d.selection ? "d.selection.createRange().text = " + d.selection.createRange().text : "d.getSelection() = " + d.getSelection();
    }
}

Open in new window

Here's a screen capture of the alerts in Firefox and Internet Explorer 8:
captureBy the way, I didn't test the other part of the function regarding frames but assume it would work if the document contains frames and the 'document' itself would not contain a selection.
AdamSenior Developer
Commented:
The statement:

(d.selection) ?

tests to see if the property'selection' exists for the document object (d references the document object)

If it does (and it only exists in IE) then d.selection.createRange().text is returned.

If it does not - all browsers except IE - then the result from d.getSelection() is returned.

Author

Commented:
Thanks for the responses.  So the only time d.selection will have a value is if IE is the browser.  I've been using Chrome lately, so now this makes sense.

After testing in the 3 browsers, I see that the test for !s seems redundant since s=='' applies to all 3 browsers(?)

>> By the way, I didn't test the other part of the function regarding frames but assume it would work if the document contains frames and the 'document' itself would not contain a selection.
He He!  I am trying to test that right now.  If you know how help me do that, please respond to my post http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27825607.html

By the way, is it this inconsistency between browsers the reason to use something like jquery?
Robert SchuttSoftware Engineer
CERTIFIED EXPERT

Commented:
> After testing in the 3 browsers, I see that the test for !s seems redundant

There's a lot of discussion on the net on how and why you should test for empty strings/objects in javascript.

If you've tested it and are happy with it you can leave the "!s" out of course but in general you shouldn't test for an object value directly if it can contain null if only because not everybody uses the latest version of 1 of 'the 3 browsers' (and there are more than 3 browsers ;-)

> please respond to my post ...

I'll have a look.

Author

Commented:
>> in general you shouldn't test for an object value directly if it can contain null if only because not everybody uses the latest version of 1 of 'the 3 browsers' (and there are more than 3 browsers ;-)

k thanks for the education -- much appreciated!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.