Input type image not redirecting

hi

Can anyone tell me why the below code is only alerting “HELLO”. It is not redirecting me to google.com


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="get">
<a href="#" onclick="alert('Hello');window.location.href='http://www.google.com';"><input type="image" name="but" src="htc_m8_slide.jpg" /></a>
</form>
</body>
</html>

I even tried the below diferent code. But this also doesnt redirect me to google.com


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="get">
<a href="http://www.google.com" target="_blank"><input type="image" name="but" src="htc_m8_slide.jpg" /></a>
</form>
</body>
</html>

Vineet

Try adding return false as follows, this prevents the click from carrying out its default action, which in this case is to append the cursor click coordinates to the location.href. Also, try changing the false to true. You will then see the default action in the location address.


<form action="" method="get">
<a href="#" onclick="alert('Hello');window.location.href='http://www.google.com';[COLOR="#FF0000"][B] return false;[/B][/COLOR]"><input type="image" name="but" src="htc_m8_slide.jpg" /></a>
</form>

Get rid of the <a> tag from around the <input type=“image”> as it will never run as clicking on the image will submit the form.

<form action="http://www.google.com" method="get">
<input type="image" name="but" src="htc_m8_slide.jpg" />
</form>

Well not quite never but you’d need to return false from the onsubmit handler (or equivalent from the submit event listener) to not trigger the action on the form first.

Hi

Thanks AllanP

Your code works for me

Vineet