views:

48

answers:

3

I have an <a> tag that contains a span. The span has javascript attached to it that does something. As a result, when the span is clicked, the javascript runs, but the <a href> is ignored and it doesn't navigate to it.

Is there a way to make it continue to navigate as normal?

<a href='http://www.google.com'&gt;&lt;span&gt;some text</span></a>

the click event is actually registered some other way than onclick, my question title is a bit misleading.

Hovering over the link puts the URL into the status bar as you'd expect, just clicking it does nothing.

The javascript is simple return true; at the moment while I try to figure it out.

I suspect as the javascript is in the span and not the a that it gets confused about how to propogate back up to the href once the javascript is done.

I'm afraid I don't know how it's bound, because it's some bizarre third party component.

A: 

Your javascript is probably returning a value that is non-true (a value that evaluates to boolean false). The simple solution is to add a return true statement to the end of the function/code that runs when the link is clicked.

tj111
Tried this but no luck
SLC
Can you post the link html as well as the javascript that is run (and the code that attaches it to the link)?
tj111
Unfortunately I don't know how it's running the javascript when I click, it's a monstrous sharepoint page of doom, it must be registering it somehow.
SLC
In the profiling tab in Firebug there's an option for profiling js in between intervals. Enable that, click the link, then disable it. It will show all the javascript that was called in that time span (which should only be the link).
tj111
A: 

If you're using jQuery, make sure you don't return false in your event handler. That will override the defaults.

If not: you may need to explicitly code what you want your link to do after your JS:

<a href="www.example.com" onclick="javascript:someFunction(); window.location = \"www.example.com\"">click here</a>

This will execute a function, then redirect the window to the proper location.

Tim
Sadly I can't edit the onclick event of the span, the content is pulled using an CMS and automatically generated.
SLC
ouch for downvotes... what's so wrong? Is it that I used the onclick attribute instead of moving it to a separate JS file? is it that I used the javascript: pseudo-proto?
Tim
If you can't edit the click event of the span, you may be in a world of hurt. You might have to grab the function by reference, clear the span of its click event, and add a new event that calls the old reference and then returns true afterwards.
sworoc
I can edit the method but not the html...confusing eh:) Not sure who did the downvotes, wasn't me.
SLC
+1  A: 

I suspect as the javascript is in the span and not the a that it gets confused about how to propogate back up to the href once the javascript is done.

Yes. If your onClick handler is on the span, the browser can't read the hyperlink's href attribute. If the link has an id, you can do :

function onclick()
{
    //Do your things...

    var link = document.getElementbyId('your_link_id').href;
    window.location.href = link;
    return true;
}
Squ36
Sounds about right, now to figure out how to do it when I don't know the ID... I can use `this` keyword and get the parent.href (the `a` tag) perhaps?
SLC
I don't believe that's actually true. Unless the event handler on the span is somehow preventing the click event from bubbling, the default onclick for the <a> should still fire. See http://jsfiddle.net/3Hk4m/ for a quick example.
Daniel Vandersluis
I agree, it is probably something else, but the proposed hack should work I just need to figure out how :)
SLC
You should use jQuery, it makes your life easier :p
Squ36