?
Solved

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

Posted on 2012-08-11
10
Medium Priority
?
487 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:SAbboushi
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:Adam
ID: 38283982
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
0
 

Author Comment

by:SAbboushi
ID: 38284001
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?
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 38284051
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!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:SAbboushi
ID: 38284150
>> 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?
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 1200 total points
ID: 38285096
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).
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38285114
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.
0
 
LVL 12

Assisted Solution

by:Adam
Adam earned 800 total points
ID: 38285263
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.
0
 

Author Comment

by:SAbboushi
ID: 38285583
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?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38285781
> 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.
0
 

Author Comment

by:SAbboushi
ID: 38285877
>> 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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question