Tag Archives: window

Javascript Debug Window Script CodeUnit 07 JUN 2010

I found this little snippet of code floating around the Internet and despite its age, it still proves to be quite a handy little guy to have around when you are looking to quickly pop in a debug window in order to test some values against when in the middle of debugging some of that existing code of yours.

The premise for the script is pretty simple. Create a popup window and direct all debug comments into the new “debug” window, meaning that your site display remains clean and tidy but at the same time you’re are getting all the hidden variable values you really care about.

So no more print_r and annoying alerts to break the flow of your web application in other words! :P

// Show the debug window
function showDebug() {
  window.top.debugWindow =
      window.open("",
                  "Debug",
                  "left=0,top=0,width=300,height=700,scrollbars=yes,"
                  +"status=yes,resizable=yes");
  window.top.debugWindow.opener = self;
  // open the document for writing
  window.top.debugWindow.document.open();
  window.top.debugWindow.document.write(
      "Debug Window
n");
}

// If the debug window exists, then write to it
function debug(text) {
  if (window.top.debugWindow && ! window.top.debugWindow.closed) {
    window.top.debugWindow.document.write(text+"n");
  }
}

// If the debug window exists, then close it
function hideDebug() {
  if (window.top.debugWindow && ! window.top.debugWindow.closed) {
    window.top.debugWindow.close();
    window.top.debugWindow = null;
  }
}

To use is pretty simple really. At the start of your page, generate the debug window by calling showDebug(). Then, whenever you want to print something out, simply make use of the debug function, a little like this: debug(“this is a debug message”). Finally, if you are a purist, you may as well close your popup debug window by using hideDebug().

Of course, today’s awesome tools like Firefox’s plugin extraordinaire Firebug and even Internet Explorer’s built in debugger makes debugging Javascript pretty simple, but still, it remains pretty useful when you want to do something yourself and that little something you want to check doesn’t quite fall under the auspices of the existing tools at hand! :)

How to Scroll to the Top of a Page with Plain Old Javascript CodeUnit 19 MAY 2010

Sometimes a person forgets the simple things in life, so every now and then it is a good thing to be reminded of them. Like scrolling to the top of a window for example.

Plain old vanilla Javascript comes equipped with two really handy functions, namely scrollBy(dx,dy) which allows you to scroll a window by the specified number of pixels and scrollTo(x,y) which forces a window to scroll to the specified co-ordinates, with the specified point referring to the top left corner of the window.

Now with this said, it becomes painfully obvious how you would scroll to the top of a window using Javascript alone. Simply call:

window.scrollTo(0,0);

As you can figure out, this simply tells the window to draw itself with the top left corner sitting at points 0,0, in other words, at the top of the screen.

Of course, this could be extended to say for example, “locking” the screen while a page loads – in other words deny the scrolling ability to a user while a page’s contents is still in the process of loading.

To do this we will make use of the handy setInterval function:

<html>
<head>
<script type=”text/javascript”>
var lockit=setInterval(“window.scrollTo(0,0)”,10)
</script>
</head>
<body onLoad=”clearInterval(lockit)”>
</body>
</html>

Simple, but useful to be reminded of it, no? ;)

Screenshot Saturday: Michiko to Hatchin (Episode 1) Anime 09 JAN 2010

Michiko to Hatchin is one roller-coaster of a ride in terms of anime series, always keeping fresh, always pulling out the surprises and genuinely managing to stay original and thoroughly enjoyable. Hence it is no wonder it managed to sneak in for a Screenshot Saturday showing!

Funnily enough, a window will break when you jump a scooter through it...

Towards the end of the first episode of Michiko to Hatchin we have this absolutely fantastic sequence where Michiko comes careening out of nowhere on her over-powered turquoise super scooter, ramping through the big glass window into the unsuspecting evil foster parents’ dining room in order to locate her missing daughter Hatchin.

Now up to this point we’ve been subjected to the sad abuse that poor little Hatchin has had to contend with for each and every day of her miserable life here with the rather worldly priest and his family and so one can’t help but feel utter elation at this sudden and unexpected burst of a ray of sunshine, but at the same time, one can’t help but wonder just what the heck Hatchin has to deal with now.

After all, Michiko is rude, crude, an escaped convict, doesn’t flinch even when bullets whiz by (the priest is a lousy shot anyway) and couldn’t give a toss about what anybody might think of her – in other words not exactly the perfect, happy escape from her life of misery that young Hatchin was hoping for after all…

Um no. No, it's not me you're looking for, honest!

Related Link: http://en.wikipedia.org/wiki/Michiko_to_Hatchin