Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1751 Why do animated GIFs stop animating when I click on a link, and how can I avoid this?

You are here: irt.org | FAQ | JavaScript | Image | Q1751 [ previous next ]

When you click on a link, or slick on a submit button, the deafult action that the browser takes is to load the page indicated by the href attribute, or submit the form to the service specified in the target attribute. When you do this the browser stops the animated image/s. To avoid this you can use either the onClick event handler (in a link) or the onSubmit event handler (in a form) to cancel the default action by returning false. This also stops the browser from loading the page or from submitting the form.

You can however still load a page using JavaScript by changing the location object's href attribute:

<html>

<head>

<script language="JavaScript"><!--
function go(href) {
  location.href = href;
}
//--></script>

</head>

<body>

<a href="defaultAction.htm" onClick="go(this.href);return false">go</a>

<form target="defaultAction.htm" onsubmit="go(this.target);return false">
<input type="submit" value="Submit">
</form>

</body>

</html>

Beware! If you attempt to use a javascript: schema within a href attribute, thinking that this might also work - then it won't. By the time the javascript: schema within the href attribute, and its associated JavaScript code is executed, the browser will have already stopped the animation.

Feedback on 'Q1751 Why do animated GIFs stop animating when I click on a link, and how can I avoid this?'

©2018 Martin Webb