I am Batman
Batman

JavaScript Binary Clock

This is just some doodle I create just now. I have always wanted a binary watch (I have even seen it in an online shop), but I need to save money (too much spending already). So in the meantime, I think “Why don’t I just create one?” using code of course. So I write this simple script:

Put this HTML code.

<pre>
	<div id="full"></div>
	H: <span id="hour"></span>
	M: <span id="minute"></span>
	S: <span id="second"></span>
</pre>

And this JavaScript code in one page.

h = document.getElementById("hour");
m = document.getElementById("minute");
s = document.getElementById("second");
f = document.getElementById("full");

function str_pad(s, n, p) {
	if (p == undefined) p = '0';
	while (s.length < n) {
		s = p + s;
	}
	return s;
}

function int_to_bin(i) {
	var b = '';
	i = parseInt(i);
	while (i > 0) {
		b = (i % 2) + b;
		i = parseInt(i / 2);
	}
	return b;
}

setInterval(function() {
	var d = new Date;
	h.innerHTML = str_pad(int_to_bin(d.getHours()), 6);
	m.innerHTML = str_pad(int_to_bin(d.getMinutes()), 6);
	s.innerHTML = str_pad(int_to_bin(d.getSeconds()), 6);
	f.innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}, 1000);

And you will see something like this.

March 9th, 2011 JavaScript Tags: , 0 Comment 594 views

View the Current Page’s Current Source Code

Do you know that when you choose to “View Source” in your browser (Ctrl+U in Firefox). You will see the page source as it is delivered by the server. Any change made by the client-side script will not be seen. So, how do we see the current page CURRENT source code? I have been using Firebug for this, but just now I see somebody post the JavaScript code for this in kaskus (Largest Indonesian Community).

Here is the code he use:

javascript:h=document.getElementsByTagName('html')[0].innerHTML;function%20disp(h){h=h.replace(/</g,'\n&lt;');h=h.replace(/>/g,'&gt;');document.getElementsByTagName('body')[0].innerHTML='<pre>&lt;html&gt;'+h.replace(/(\n|\r)+/g,'\n')+'&lt;/html&gt;</pre>';}void(disp(h));

At a quick glance, this script will replace all “<” with “&lt;” and all “>” with “&gt;” inside the “<html>” tag thus removing all tags and make those tags readable.

Let’s break down the code: Read the rest of this entry »

February 22nd, 2011 JavaScript Tags: , , , 0 Comment 815 views