views:

155

answers:

5

I'm building a very simple web-based file browser for my website.

I've styled my links to look like files and I want to be able to single-click to select them so I can do things like rename etc, and I want to be able to double-click to actually activate the link and download the file.

I've come up with the following, but it looks ugly. Does anyone have a more elegant solution?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>

<script type="text/javascript">

function nullClick()
{
    //do select stuff
    return false;
}

function dolink(link)
{
    window.location(link.href);
}

</script>

</head>

<body>

<a href="http://pathtofile" onclick="return nullClick();" ondblclick="dolink(this);">Clicky</a>

</body>
</html>
+4  A: 

I would avoid doing this since it goes against how users expected web sited to function, but if you must, the way you do it is the only way I know of.

Also, you should know that the site will fall back to single-clickable links if javascript is disabled or unavailable.

Ben S
I agree, the site it really only for my own use, and the `nullClick` method would actually restyle the link to look 'selected'.
MalphasWats
He needs more than to just ignore the onclick, so the nullClick() function does make sense in the example. Read the question again - "I want to be able to single-click to select them so I can do things like rename etc"
Saul Dolgin
I see, then ignore my previous comment about removing the function. I've edited my answer.
Ben S
Thanks Ben, I'm intending to progressively enhance the filebrowser anyway, so it already behaves sensibly on single-click (loads a new page with fileinfo and functions), just wanted to check if there was a better way of doing it really.
MalphasWats
A: 
<a href="http://pathtofile" onclick="return false" ondblclick="window.location = 'some where'">Clicky</a>

I think it would be more elegant solution with one line of code.

Braveyard
A: 

If you're not using any JavaScript framework, that is probably as good as it gets. You could get rid of your functions thou;

<a href="..." onclick="return false;" ondblclick="location.href='...';">Foobar</a>
Björn
+1  A: 

If you're not opposed to a little jQuery:

$("#yourLinkId").dblclick(function () { 
      window.location($(this).attr("href")); 
    });
Joel Martinez
not a fan of jQuery, feels too much like cheating :)
MalphasWats
I know what you mean, but after using it for five minutes, I was sold :)
Thomas
Why not cheat when you can?
Ben S
It's also cheating if you require doubleclicks in links.
BalusC
I enjoy the challenge and I learn a lot more rolling my own where I can! @BalusC I take the point, it was more of a shortcut so I can do things to the files and still download them without a page load.
MalphasWats
You can do the same in almost as little code using plain JavaScript, assigning to the `ondblclick` property.
bobince
A: 

How about using jQuery?

HTML example:

<a href="javascript:alert('link1')" class="dblclick">link1</a><br>
<a href="javascript:alert('link2')" class="dblclick">link2</a><br>
<a href="javascript:alert('link3')" class="dblclick">link3</a><br>

jQuery example:

$('a.dblclick')
    .bind('click', function() { return false; })
    .bind('dblclick', function() { window.location = this.href; });

SSCCE:

<!doctype html>
<html lang="en">
    <head>
        <title>Test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(document).ready(init);
            function init() {
                $('a.dblclick')
                    .bind('click', function() { return false; })
                    .bind('dblclick', function() { window.location = this.href; });
            }
        </script>
    </head>
    <body>
        <a href="javascript:alert('link1')" class="dblclick">link1</a><br>
        <a href="javascript:alert('link2')" class="dblclick">link2</a><br>
        <a href="javascript:alert('link3')" class="dblclick">link3</a><br>
    </body>
</html>
BalusC