Ryan
Ryan

Reputation: 21

Javascript onload run function help

Need help with my simple script that is supposed to auto click next after 5 seconds.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
</title>
<script type="text/javascript">
window.onload = Next() {
    setTimeout(Next() {
        var next = document.getElementById("NEXT");
        window.location.href = next.href;
    }, 5000);
}
</script>

</head>
<body>
<div align="right">
<a id="NEXT" href="http://www.mysite.com/pictures.php?id=34">[ NEXT ]</a>
</div>
</body>
</html>

Upvotes: 2

Views: 454

Answers (2)

Raynos
Raynos

Reputation: 169383

Your problem is that .click() only works on buttons.

Whilst were at it lets use unobtrusive javascript.

window.onload = function() {
    setTimeout(function() {
        var next = document.getElementById("NEXT")
        window.location.href = next.href;
    }, 5000);
}

Live example.

Edit

window.onload = Next() {
setTimeout(Next() {

Don't use the word Next() just use function()

To create functions you need either function () or function SomeName()

Upvotes: 3

On a point of correctness, your script doesn't say it's javascript (you need to say which scripting language it is), and your html technically doesn't say it's HTML (it's missing a doctype declaration):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Title of the document</title>
  <script type="text/javascript">
    function next() {
      // '1' dynamically generated when this page was generated by PHP,
      // and will be '2' next time the page loads.
      location = "pictures.php?id=1";
    }
    document.addEventListener("DOMContentLoaded", function(){setTimeout(next,5000);}, false);
  </script>
</head>
<body>
  ...
</body>
</html>

This sounds like being pedantic, but IE in particular is really anal about having those things. Without a doctype declaration, it won't treat a document starting with as HTML code, but start to take fairly inaccurate guesses.

Upvotes: 0

Related Questions