Tony_Henrich
Tony_Henrich

Reputation: 44225

Need to go to a named link and execute a javascript at the same time

I have a hyperlink which goes to a named link. <a href="#question"> I needed it also to execute a Javascript so I did:

`<a href="javascript:SomeFunction();#question">`

which didn't work. I guess I have to jump to the named link from Javascript. How do I go to named link from Javascript?

Upvotes: 0

Views: 133

Answers (4)

Dmitri Farkov
Dmitri Farkov

Reputation: 9691

window.location = "location here"

this will redirect to whatever url

EXAMPLE: window.location = "#header"

OR if using a framework like jQuery

$('a.cool').click(function(){
    someFunction(this);
    window.location = "#hash";
    return false;
});

Upvotes: 1

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94284

Put the named anchor in the href and either use onclick or register the event on the <a> through javascript.

So either:

<a href="#question" onclick="SomeFunction();">

Or (this example makes use of prototype as an example, you could do the same with other javascript frameworks or with pure javascript):

<a href="#question" id="question_link">
<script type="text/javascript">
  $('question_link').observe('click', SomeFunction);
</script>

Upvotes: 2

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171559

Try:

<a href="javascript:SomeFunction();document.location='#question'">

Upvotes: 1

Jacob Mattison
Jacob Mattison

Reputation: 51072

Window.location.hash="question"

Upvotes: 1

Related Questions