views:

1642

answers:

7

Hi everyone,

I am in charge of a website at work and recently I have added ajaxy requests to make it faster and more responsive. But it has raised an issue.

On my pages, there is an index table on the left, like a menu. Once you have clicked on it, it makes a request that fills the rest of the page. At anytime you can click on another item of the index to load a different page.

Before adding javascript, it was possible to middle click (open new tabs) for each item of the index, which allowed to have other pages loading while I was dealing with one of them. But since I have changed all the links to be ajax requests, they now execute some javascript instead of being real links. So they are only opening empty tabs when I middle click on them.

Is there a way to combine both functionalities: links firing javascript when left clicked or new tabs when middle clicked? Does it have to be some ugly javascript that catches every clicks and deal with them accordingly?

Thanks.

A: 

Possibly, I could provide two links each time, one firing the javascript and another being a real link that would allow for middle click. I presume, one of them would have to be an image to avoid overloading the index.

Franck Mesirard
+1  A: 

It would require some testing, but I believe that most browsers do not execute the click handler when you click them, meaning that only the link is utilized.

Not however that your handler function needs to return false to ensure these links aren't used when normally clicking.

EDIT: Felt this could use an example:

<a href="/Whatever/Wherever.htm" onclick="handler(); return false;" />
Guvante
+10  A: 

Yes. Instead of:

<a href="javascript:code">...</a>

Do this:

<a href="/non/ajax/display/page" id="thisLink">...</a>

And then in your JS, hook the link via it's ID to do the AJAX call. Remember that you need to stop the click event from bubbling up. Most frameworks have an event killer built in that you can call (just look at its Event class).

Here's the event handling and event-killer in jquery:

$("#thisLink").click(function(ev, ob) {
    alert("thisLink was clicked");
    ev.stopPropagation();
});

Of course you can be a lot more clever, while juggling things like this but I think it's important to stress that this method is so much cleaner than using onclick attributes.

Keep your JS in the JS!

Oli
The only "problem" with this approach is that someone reviewing the markup won't be able to immediately see that there's an associated event handler.
Rob
I wouldn't class that a problem. If you structure things in a meaningful way, you should be able to look at the JS, see where all the event hooks are. If you're worried too much about it, you *could* set the rel attr to js, or prepent the ID with js_, etc.
Oli
Or alternatively you could use the "onclick" attribute as it's intended and place the name of the event handling function in there. Re-purposing the "rel" attribute, or prepending the ID with js_ really does "smell bad" when there's an attribute defined expressly for the purpose.
Rob
I only suggested them because you said it made things harder to review, but if I want to see what's happening in JS, I'll look in the JS rather than the HTML. Trying to hunt through HTML for JS issues smells a lot more.
Oli
A: 

The onlick event won't be fired for that type of click, so you need to add an href attribute which would actually work, possibly by adding a #bookmark to the URL indicate to the target page what the required state is.

Paul Dixon
It does scuzz up the markup though. I'd vote for giving it an id and leaving the JS in the JS, where it belongs any day of the week.
Oli
+3  A: 

Yes, You need to lookup progressive enhancement and unobtrusive Javascript, and code your site to work with out Javascript enabled first and then add the Javascripts functions after you have the basic site working.

Lewis Jubb
+1  A: 
<a href="/original/url" onclick="return !doSomething();">link text</a>

For more info and detailed explanation view my answer in another post.

Fczbkk
A: 

Hi guys,

How would you do it for this example:

<td width="67" onClick="document.location.href='http://www.domain.com';" style="cursor:pointer"></td>

At the moment it's a table cell link but I can't open it in a new tab when I middle click it.

Thanks,

Dan