-1

I am experimenting with JavaScript again, and wanted to make a clock in an alert box (bookmarklet).

The following code:

javascript:alert(new%20Date().toLocaleString())

will show the time in the format Wednesday, August 08, 2012 12:40:54 AM. However, the time for the clock is not updated because the bookmarklet is just fetching the time. So, are alert boxes able to be updated (like this:)

javascript:alert(setInterval("new%20Date().toLocaleString()",1000))

or is an alert box not able to be updated (it fetches something and then stops executing script)?

EDIT* I have tried the second code line already and know it doesn't work, I only mean for it to be an example of finding a method that allows the time to be updated.

Ylj
  • 45
  • 1
  • 7

3 Answers3

0

That's correct. Alert boxes cannot be updated. They also posses another very important feature which is difficult to achieve otherwise in javascript (not that you would want to). They completely suspend execution of any code (because javascript is single threaded) until they are dismissed.

Strelok
  • 50,229
  • 9
  • 102
  • 115
0

No - alert boxes print a string and their content cannot be changed whilst they are showing.

Use a DHTML dialog simulation instead, such as Facebook's. Since these are just HTML doing the job of native modal functions (e.g. alert()) they are much more flexible.

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Is there a way to create a modal window (e.g. [modal dialog scripts](http://www.wingo.com/dialogbox/index.html) and [Amazon link example](http://stackoverflow.com/questions/11651905/load-a-modal-window-from-a-bookmarklet-like-the-amazon-wishlist-bookmarklet)) in a bookmarklet without having to use an external JS file (using javascriptcompressor.com or something) in order to accomplish this? – Ylj Aug 08 '12 at 08:56
  • Yes - you would simply have to do all the work in the bookmarklet, i.e. creating, styling and appending the DIV (presumably), and adding interaction. – Mitya Aug 08 '12 at 09:14
0

Here is a working example of a javascript clock from github.

He sources the tutorial he used in his Readme.

lwm
  • 192
  • 1
  • 10