How to execute javascript code directly in your browser

This trick is not known by everyone though it is really simple.

The address bar

In the address bar, you simply write

  1. javascript:/*some javascript code here*/;void(0);

As easy as that.

In fact, it’s the same thing as creating a link that executes some javascript code.

  1. <a href="javascript:alert(‘Some code from a link’);">Execute code from javascript</a>

The only thing I add is the void(0); at the end of the code to avoid a “bug” that reloads the page.

Hey, if you want to write a bookmarklet, you’ll probably use this technique.

Firebug

These days, you can’t write javascript without using Firebug (apart if you are a javascript magician). You just can’t. For the ones who don’t know Firebug, I’ll describe it in 7 words : javascript debugger, html inspector, css modifier, fun.

On the “Console” tag, there’s one line beginning with “>>>” at the bottom. This is the javascript executer. If you click the red arrow at the end of the line, you have a multi-line executer.

I always use it to write and test the javascript snippets on this site.

What can I do with that?

Everything you want. In fact, I use it for these purpose :

  • write code snippet for Javascript Kata
  • write a bookmarklet
  • debug javascript on Firefox
  • have fun on other’s sites
  • BK

    How do you debug JS in IE using Firebug? Isn’t Firebug only for FF?

  • http://www.javascriptkata.com/ Dan

    Firebug is Firefox only! But every serious programmer knows you can’t use IE to debug ;)

  • http://breakfastdinnertea.co.uk Si

    Firebug’s got a decent JS executer – but the Shell bookmarklet wipes the floor with it : http://www.squarefree.com/bookmarklets/webdevel.html#shell

    one-word: code-completion

  • http://www.javascriptkata.com/ Dan

    Thanks Si, these are cool tools!

  • http://www.shifteleven.com K. Adam Christensen

    If you want to debug in IE, or Safari for that matter, try Firebug lite.

  • http://www.javascriptkata.com/ Dan

    @Adam
    I had a look at Firebug Lite. It is cool but I naively thought it would have an integrated debugger.

    The question is : can we have an IE debugger (apart of Visual Studio) that reproduces the same kind of debugging than Firebug?

  • BK

    I never used the debugger in firebug but if you want a debugger for IE, you could use MS Script Debugger but the problem is that you need to be genuine. For information on debugging IE, you may look here:
    (http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx)

  • JavaScript Help

    Can anyone explain why or how can javascript codes are executable in the address bar of a web borwser?

  • http://www.javascriptkata.com/ Dan

    As I said in the post :

    ===========
    In fact, it’s the same thing as creating a link that executes some javascript code.

    Execute code from javascript
    ===========

  • http://kourge.net/ kourge

    javascript: is a pseudo-protocol implemented by browsers in the early days of the web.

  • http://www.koiblue.com Chad

    >>Everything you want. In fact, I use it for these purpose :
    >> * debug javascript on IE

    >>>Dan (maintainer of Javascript Kata) May 1 2007 at 09:50:17
    >>>Firebug is Firefox only! But every serious programmer knows
    >>>you can’t use IE to debug ;)

    HUH??? If you are using it on FF how are you debugging on IE?

  • http://cookiecrook.com/ James Craig

    Dan, perhaps you could recommend Firebug Lite for all the people asking to debug in Ineternet Explorer. You can debug in IE, it’s just much harder.

    Also, the page reload isn’t a bug as you claimed. It’s the output of the function you called. Using the javascript psuedo-protocol effectively turns the browser into the console, therefore “javascript:(2+2)” will output “4″ just as it should. The void function just tells the location field to have no output so the page isn’t rewritten.

  • http://www.javascriptkata.com/ Dan

    @James
    thanks for the info.

    @Chad
    Like James said, have a look at Firebug Lite. It doesn’t have all the features from the real Firebug but it can be handy.

    I know that Microsoft offers a javascript debugger that can be installed on your computer but I never worked with it and I don’t know what it’s worth. If you try it, tell us about it…

  • http://www.koiblue.com Chad

    I appreciate the recommendation, but I think you are misunderstanding my question. I am not asking for ways to debug in IE. I am asking for you to explain what you meant in the original post when you stated that you used firebug to debug javascript for IE.

  • http://www.javascriptkata.com/ Dan

    @Chad
    Oops… I made a terrible mistake. I meant “debug javascript on Firefox”. It is now changed.

  • http://www.koiblue.com Chad

    LOL Ahh the mystery is solved :) I was hoping there was some amazing feature that let firebug work with IE magically.

  • Riya

    how can i run a simple js file stored in local system.

  • http://codeoptimism.net Chris

    Hey thanks for the note about void(0);, I was making a setTimeout(); bookmarklet in IE8 and didn’t know wtf was going on, I’m quite grateful. :) (The application is rather important to me!)

  • Rex the Strange

    Firebug is Firefox only! But every serious programmer knows you can’t use IE to debug

    Wrong! Visual Studio includes an integrated IE javascript debugger.

  • http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/ jas

    How can I repeatedly execute a code in JavaScript?

  • gayathri

    How can I repeatedly execute a code in javascript

  • Mazyar

    How can I run JScript that is stored in a URL in the address line?

  • http://www.timmyontime.com Dan Simard

    It's explained in that post. You simply write javascript:alert(“your code”);void(0); and it works!

  • mazyare

    My question is about javascript that is stored in a URL. for e.g. http://my.site.com/long-multiline.js

  • http://www.timmyontime.com Dan Simard

    Take a look at http://www.javascriptkata.com/2007/04/19/how-to… in the “How to import another JS in a bookmarklet” section.

  • Vincent_esti

    god day sir!..
    i want to a javascript code to be executed whenever someone clicked a specific part of a webpage(not my own created webpage)…..

    is that possible??? without using the address bar??
    thank you!…

  • http://www.javascriptkata.com Dan Simard

    It's impossible because it would break the security. Maybe you should take a look at something like GreaseMonkey (http://www.greasespot.net/).

  • purushotham

    who will take the responsibility of execution of javascript under brower

  • Concerned Citizen

    javascript:(function(){ var x; for( x=0; x < 100; x++){alert(x);} })();

  • darshan

    is there any mechanism that hide the actual url of the website and display another url , like http://www.google.com / in javascript?

  • JC

    how do I easily run an entire file of Javascript whenever I open any webpage? Thanks!

  • Maw

    That void(0); really helped me out a lot. I used javascript on a website with terrible pagination so I can get to a specific page without clicking next 100 times.

  • http://www.ngumbi.com/ mikato

    It looks like this isn’t allowed anymore, at least by Firefox and IE.

  • http://www.javascriptkata.com dsimard

    yeah, this post is quite old (almost 4 years) and I heard something like that too…

  • Farjad4g

    document.getElementById(‘dsq-comments’)=null;

  • http://kalzdjsdcvi.com/ dYcxcgcvf

    2011…

    Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantas…

  • http://kalzggdjsdcvi.com/ dYcxcgcvfgg

    2011…

    Pretty! This was a really wonderful post. Thank you for your provided information….

  • http://Blzggzdjsdscvi.com/ dYcbbgx

    2011…

    I don’t even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you’re going to a famous blogger if you aren’t already ;) Cheers!…

  • http://www.black-strapless-dress.net Black Strapless Mini Dress

    Black Satin Strapless Dress…

    [...]How to execute javascript code directly in your browser » JavascriptKata[...]…

  • http://Blzggzdjsdsccvi.com/ dYcbbgcx

    2011…

    Hello there, just became aware of your blog through Google, and found that it’s really informative. I am gonna watch out for brussels. I’ll appreciate if you continue this in the future. Many people will be benefited from your writing. Cheers!…

  • http://esoftwarereview.info software reviews

    2011…

    I am really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it’s rare to see a great blog like this one today…..

  • http://vilumparminavilumn.com/ Parminavilum

    Cool sites…

    [...]we came across a cool site that you might enjoy. Take a look if you want[...]……

  • http://mariuniumopsz.com/ Irimaniumnops

    Cool sites…

    [...]we came across a cool site that you might enjoy. Take a look if you want[...]……

  • http://www.sportspicksnation.com NFL picks against the spread

    2011…

    I like the helpful information you provide in your articles. I will bookmark your blog and check again here regularly. I’m quite certain I will learn a lot of new stuff right here! Good luck for the next!…

  • http://www.banzacorp.net banzai

    I received an e-mail about starting a website for a small business. I don’t yet have a business but I would like to start my own personal website…..

    How do you compute the start-up costs for starting a website?…