0

I have an object that's being created on the body onload event:

<body onload="var clock = new Clock(<?php echo $user->timezone ?>); clock.start();">

when I try to reference it further on the page, lets say on this:

<div id="month" onclick="alert(clock.time());"></div>

It will give me the error of undeclared function.. am I missing something?

ps: I don't really want to instantiate the object somewhere else in the javascript file, since clock takes a couple arguments from PHP.

mTorres
  • 3,590
  • 2
  • 25
  • 36
sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • 3
    Your `onload` code is placed inside a function that is bound to `window.onload`. Because of this, the `var clock` creates a *local* variable that isn't accessible outside that function. If you need `clock` to be global, then do `window.clock = new Clock(...` instead. – cookie monster Aug 05 '14 at 16:52
  • Scope in inline event handlers [is absolutely odd](http://jibbering.com/faq/names/event_handler.html). Just [don't use them](http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice) – Bergi Aug 05 '14 at 17:28

0 Answers0