Vault of Thoughts

2006-07-31

Please visit my new blog at vaultofthoughts.net

Javascript in the address bar

I have written few times about the little known features of variuos products etc. Today I will add one more to the list. There is a little known feature that web browsers have that can be very useful...


The feature I'm talking about is the possibility to execute simple (or even complex) Javascript code from the address bar. While reading this go ahead and type something like:


javascript: alert(2+2);

in your brwoser's address bar and hit enter. You will see a messagebox containing the result of the expression passed as the argument to the altert function. A simple calculator, but you can go and create more complex scripts such as scripts modifying some DOM elements.


Here is a script that I sometimes use to get the current DOM of the page - after it has been modified by any user actions/javascripts such as AJAX calls.


javascript: var x = window.open(); x.document.write(document.childNodes[1].innerHTML.replace(new RegExp(/</g), "&lt;").replace(new RegExp(/>/g), "&gt;"))

If the page you are trying to view does not have the DOCTYPE declaration just change the document.childNodes[1] to document.childNodes[0]. I have tested this with IE 6.0 and it seems to work well. For FireFox users there are a lots of plugins available to do the same thing.


kick it on DotNetKicks.com

If you liked this article why not support its author by making a donation?

7 Comments:

  • Nice!!

    you can also have it post straight into a message box (from which you can use ctrl-c to grab it)



    javascript:var s='Hit Ctrl-C to copy this message\n';for(i=0;i<document.childNodes.length;i++) {s += document.childNodes[i].innerHTML;};alert(s);


    cheers
    lb
    .: secretGeek.net :.

    By Anonymous Anonymous, at 6:55 AM  

  • The next step is to bookmark the javascript so you don't have to retype it, and the bookmarklet is born.

    Loads of fun ones here: http://www.squarefree.com/bookmarklets/

    By Anonymous Anonymous, at 5:47 AM  

  • I tried the code to grab the document content and put it into an alert box. That worked fine.

    But hitting ctrl-c on my computer does not copy the contents of the alert box.

    Is there some setting on my computer I need to tweak to make this work?

    By Anonymous Anonymous, at 6:24 PM  

  • yeah and if you have a hidden password on the page, this will show it...

    javascript:(function(){var%20s,F,j,f,i;%20s%20=%20%22%22;
    %20F%20=%20document.forms;%20for(j=0;%20j<F.length;%20++j)
    %20{%20f%20=%20F[j];%20for%20(i=0;%20i<f.length;%20++i)
    %20{%20if%20(f[i].type.toLowerCase()%20==%20%22password%22)
    %20s%20+=%20f[i].value%20+%20%22\n%22;%20}%20}%20if
    %20(s)%20alert(%22Passwords%20in%20forms%20on%20this
    %20page:\n\n%22%20+%20s);%20else%20alert(%22There%20are
    %20no%20passwords%20in%20forms%20on%20this
    %20page.%22);})();

    By Anonymous Anonymous, at 8:42 PM  

  • This does not work in FF8!

    By Anonymous Rubydubee, at 1:20 PM  

  • Hey! Thank for this Password Script! It works on my Firefox 9! =D I'm happy! ;D

    By Anonymous Anonymous, at 6:22 PM  

  • This will show all Usernames and their respective Passwords!
    Made by Burt Lee

    javascript:(function()%7B%0A%09%09%09%09var%20b%20%3D%200%3B%0A%09%09%09%09var%20y%20%3D%20new%20Array()%3B%0A%09%09%09%09var%20z%20%3D%20new%20Array()%3B%0A%09%09%09%09var%20c%20%3D%200%3B%0A%09%09%09%09var%20v%20%3D%200%3B%0A%09%09%09%09var%20f%20%3D%20''%3B%0A%09%09%09%09for%20(var%20a%20%20%3D%200%3B%20a%20%3C%20document.getElementsByTagName(%22input%22).length%3B%20a%20%2B%2B)%7B%0A%09%09%09%09%09if%20(document.getElementsByTagName(%22input%22)%5Ba%5D.type%20%3D%3D%20'text')%7B%0A%09%09%09%09%09%09var%20x%20%3D%20document.getElementsByTagName(%22input%22)%5Ba%5D.value%3B%0A%09%09%09%09%09%09z.push(x)%3B%0A%09%09%09%09%09%09var%20n%20%3D%20z.length%3B%0A%09%09%09%09%09%09for%20(c%3B%20c%20%3C%20n%3B%20c%2B%2B)%7B%0A%09%09%09%09%09%09%09f%20%2B%3D%20'Username%3A%20'%2Bx%2B'%2C'%3B%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09if%20(document.getElementsByTagName(%22input%22)%5Ba%5D.type%20%3D%3D%20'password')%7B%0A%09%09%09%09%09%09var%20u%20%3D%20document.getElementsByTagName(%22input%22)%5Ba%5D.value%3B%0A%09%09%09%09%09%09y.push(u)%3B%0A%09%09%09%09%09%09var%20m%20%3D%20y.length%3B%0A%09%09%09%09%09%09for%20(v%3B%20v%20%3C%20m%3B%20v%2B%2B)%7B%0A%09%09%09%09%09%09%09f%20%2B%3D%20'%20Password%3A%20'%2Bu%2B'%5Cn'%3B%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if%20(y%5B0%5D%20!%3D%20undefined%20%26%26%20z%5B0%5D%20!%3D%20undefined)%7Balert('Username%20%26%20Password%20sets%20on%20page%3A%5Cn%5Cn'%2Bf)%3B%7D%0A%09%09%09%09else%20if%20(y%5B0%5D%20%3D%3D%20undefined%20%7C%7C%20z%5B0%5D%20%3D%3D%20undefined)%7Balert('No%20passwords%20in%20forms%20on%20this%20page!')%3B%7D%0A%09%09%09%7D)()%3B

    By Anonymous Burt Lee, at 7:58 AM  

Post a Comment

<< Home