Execute Javascript in URL Bar of Browser

So it seems that most of the popular web browsers will allow you to write javascript where you normally put the url address. I thought this was kinda neat.

If you put something like ‘javascript:alert(“it works”);’ into the url field of your browser you should get a popup that says ‘it works’. This can be used to change any attribute on whatever page you are currently looking at. All you need to do is browse to the desired page and put something like the following into the url bar in your browser.

javascript:document.getElementById(“title”).innerHTML=”Fake Title”;

Of course this will only work on pages that contain a div or p element with the id of title (which most sites have).
I did have some issues with this depending on the browser used. Some browsers, like firefox 3.0.10 on Ubuntu Linux would just show me a blank page with Fake Title on it. This was solved by putting the javascript statements in an anonymous function in the url line.

This is all good and fun for small little tweaks but what if you wanted to do something a little more… crafty.
It is possible to include an external javascript file and alter the page so that you can execute your own little script from a specific domain. Have you ever been playing around with some ajax and gotten an ‘Access to restricted URI denied (NS_ERROR_DOM_BAD_URI)’ error message? Well the method outlined below would allow you to put some proper ajax code into the site that isn’t allowing your requests.

While I’m not going to explain explicitly how to do anything fun I will show you how to include an external javascript file into a page in your browser that will allow you to call functions from that file on the page. Even though this will include the script you will still have to call a function from the script in the url bar. The code in the script will not execute by itself just because you included it.

javascript:{{ var e=document.createElement(“script”); alert(“here”);e.src = “http://quick-content.com/include_me.js”;e.type=”text/javascript”; document.getElementsByTagName(“head”)[0].appendChild(e);} test();}

The snippet above should all be one line. So if you paste that long one liner into the url bar of your preferred browser the javascript file from the url specified (http://quick-content.com/include_me.js) will be included into the html. At the very end of that statement I call the test() function that just changes the title of my blog to ‘Tech Stuff From Null’ as opposed to ‘Tech Stuff From Cody Taylor’. Try it out. Paste that code into your browser as you’re looking at my blog and check the title. This will work for any js file so have fun and don’t break anything.

Share

6 Responses to “Execute Javascript in URL Bar of Browser”

  • AlinO Says:

    I just tryed these javascripts you posted here but they don’t work:

    javascript:document.getElementById(”title”).innerHTML=”Fake Title”;

    javascript:{{ var e=document.createElement(”script”); alert(”here”);e.src = “http://quick-content.com/include_me.js”;e.type=”text/javascript”; document.getElementsByTagName(”head”)[0].appendChild(e);} test();}

  • AlinO Says:

    I tried in mozila firefox 3.0.11

  • Cody Taylor Says:

    Sorry. I changed my theme and the title didn’t have an id of ‘title’. It does now and it should work for you. I’m on linux so I can’t test with that version of firefox. If that still doesn’t work then try using single quotes or using internet explorer.

  • Nemon Says:

    It worked for me (firefox on ubuntu) replacing double quote with single quote.

    javascript:document.getElementById(‘title’).innerHTML=’Fake Title’;

    Just a note: writing javascript code in the url bar is very cool&fun. The question is: why does the browser render it? I can’t figure it out.

  • BurgerMeister Says:

    alert(document.cookie);

  • beginner Says:

    As suggested i changed from double quotes to single quotes and it worked. My question is what is the reason for this behavior.