DumpsterDoggy

Articles

Search
Newest Articles
Recent Tutorials
Related Blogs
Get Microsoft Silverlight
Twitter Updates

Bookmark This

Combine jQuery and Double Clicks to Get Data

Chris Missal, August 20, 2008

This example shows how you can apply jQuery and a double click to highlight a word and get some sort of information from it. I'm using a definition (if applicable) of the word. Please note that this example might not always work as expected. I wrote this just to show some more jQuery example code and to get some ideas out there. Maybe this will spark some really useful ideas for somebody.

Go ahead... double click a word... *wink*!

What is a Cookie?

A cookie is a file created by an Internet site to store information on your computer, such as your preferences when visiting that site. When you visit a site that uses cookies, the site might ask Firefox to place one or more cookies on your hard disk.

Later, when you return to the site, Firefox sends back the cookies that belong to the site. This allows the site to present you with information customized to fit your needs.

Cookies can also store personally identifiable information. Personally identifiable information is information that can be used to identify or contact you, such as your name, e-mail address, home or work address, or telephone number. However, a web site only has access to the personal information that you provide. For example, a web site cannot determine your e-mail address unless you provide it. Also, a web site cannot gain access to other information on your computer.

When you use the default cookie settings, this activity is invisible to you, and you won't know when a web site is setting a cookie or when Firefox is sending a web site its cookie. However, you can set your cookies options preferences so that you will be asked before a cookie is set. You can also restrict the lifetime of cookies to your current Firefox session.

 
<script type="text/javascript">
$(document).ready(function() {
    $("#definition button").click(function() {
        $(this).parent().hide(200);
    });
    $("#container p").dblclick(function() {
        var txt = getSelectedText();
        $.get("/php/data/define.php", { q: txt }, function(data) {
            var $dataDiv = $("<div>"+data+"</div>").addClass("data");
            $("#definition").show(200)
                .contents("h3").text("Definition of: "+txt).parent()
                .contents("div.data").replaceWith($dataDiv).parent();
        });
    });
});
function getSelectedText() {
    if (window.getSelection) return window.getSelection().toString();
    else if (document.getSelection) return document.getSelection();
    else if (document.selection) return document.selection.createRange().text;
}
</script>

Filed Under: JavaScript   jQuery