Closed pheuter closed 13 years ago
You can use the now.ready (which is immediately available, including at $(document).ready(), in order to make sure your code run as soon as NowJS is initialized.
This code will alert "Ha" without any type of hackery.
<script type="text/javascript" charset="utf-8">
now.listen = function(msg) {
alert(msg);
}
$(document).ready(function() {
now.ready(function(){
now.send("Ha");
});
});
</script>
Additionally, now does not begin to initialize until window.onload so there should not exist a race condition where now.ready(function(){...}) is created after now has initialized thus preventing now from calling the function.
Closing
Consider the following client code:
and the following server code:
Upon page load, I should see an alert box popup and display "Ha". Unfortunately, this does not happen, and I get the following error in the browser console:
Uncaught TypeError: Object #<Object> has no method 'send'
If I change the client side code to:
The alert box successfully shows up and displays "Ha".
This means that nowjs does not (completely) initialize upon dom ready, requiring a few extra moments to work. This is a problem for applications that depend on nowjs behavior upon dom initialization, and
setTimeout
seems too hacky a work'a'round.