Quines

Quines are programs whose only purpose is to print out their own source code. It cannot be done by just reading the source code from the disk and printing it, of course; the compiled program must contain all information necessary to do that.

Several years ago I was quite interested in writing quines. Not for any particular reason; it was just a cool thing to do. (In case you didn't know I am a big nerd.)

Here are some I made recently. You only need to copy and paste them into a new file, then run the program, and the source code should be output again.

HTML/JavaScript

Recently I wondered what a HTML quine would look like, so I set out to make one. In the end I didn't make only one: I made four. Three of them are legitimate quines, the fourth one was made by cheating.

  • This is the most classic, straightforward approach to making a quine in any language: just store a copy of the source in some variables using some kind of encoding, then print out everything in both encoded and decoded form. The program is 216 characters long.
    <html><body><script>u=unescape;a="<html><body><script>u=unescape;a=%22";b="%22;document.body.textContent=u(a)+a+u('%2522;b=%2522')+b+u(b);%3C/script>";document.body.textContent=u(a)+a+u('%22;b=%22')+b+u(b);</script>
  • This one uses the JavaScript eval() statement. That makes things much easier, and is in the limit of what can be considered cheating :) It is 135 characters long.
    <html><body><script>eval(c="u=unescape('%22');document.body.textContent='<html><body><script>eval(c='+u+c+u+')</'+'script>'")</script>
  • Similar to the one before it but shorter, although I think it doesn't work in Internet Explorer. 130 characters long.
    <html><body><script>eval(c="u=atob('Ig');document.body.textContent='<html><body><script>eval(c='+u+c+u+')<'+'/script>'")</script>
  • Using this last approach is cheating. It gets the contents of the page from the browser and prints them out. I'm just including it for completeness. It is 134 characters long.
    <html><head></head><body><script>d=document;d.body.textContent="<html>"+d.documentElement.innerHTML+"</html>";</script></body></html>

If you can make shorter HTML/JavaScript quines, I'd like to know about it. I know that those are not the shortest possible HTML quines. The shortest one is an empty file, which displays as an empty webpage. But that is super-cheating! :)

More to follow... eventually