views:

5081

answers:

3

I have anchor link with both href and onclick events specified. The onclick sets location.href to server url and href to a other link. When the link is clicked the href event gets called always but onclick event gets called only once (that is only for one anchor link). If i debug javascript it works properly that it calls both href and onclick.What can be the problem?

+2  A: 

Try

 <a href="whatever.html" onclick='window.location.href="http://whereverelse";return false;'>linky</a>

just to explain: returning false in this case prevents the default action.

Why would you want to do this though?

Jonathan Fingland
I guess returning false will stop href functioning.But i want both to work.Onclick does server side work like updating database after which i want to redirect the user to proper link which is specified in href.But this happens only once as stated in the problem.I tried redirecting the user from server side itself but it fails if url starts with different protocols like mms://, notes:// etc.
location.href is for changing the current page. It sounds like you want to make an AJAX request
Jonathan Fingland
+2  A: 

It sounds like you want the link to call a page that runs server-side code (such as PHP) and then direct the browser to another location. That's typically done by having the server-side script send a redirect response with the second URL.

Patrick McElhaney
A: 

I wish I had an answer but instead I have the same problem.

<img src="some.php" id="someimage" name="someimage"> <a href="Javascript://" onclick="Javascript:document.getElementById('someimage').src='some.php';" >Change</a>

it works only the first time the link is clicked. Once clicked it wont work again, even if I reload the page. the user needs to close that window and open another one.

Very strange, because it happens in Chrome but IE works fine.

What could it be ?