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

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.

<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
  • purushotham
    who will take the responsibility of execution of javascript under brower
  • 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!...
  • It's impossible because it would break the security. Maybe you should take a look at something like GreaseMonkey (http://www.greasespot.net/).
  • Mazyar
    How can I run JScript that is stored in a URL in the address line?
  • 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
  • Take a look at http://www.javascriptkata.com/2007/04/19/how-to-do-a-bookmarklet-with-javascript/ in the "How to import another JS in a bookmarklet" section.
  • jas
    How can I repeatedly execute a code in JavaScript?
  • 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.
  • 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!)
  • Riya
    how can i run a simple js file stored in local system.
  • LOL Ahh the mystery is solved :) I was hoping there was some amazing feature that let firebug work with IE magically.
  • Dan
    @Chad
    Oops... I made a terrible mistake. I meant "debug javascript on Firefox". It is now changed.
  • 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.
  • 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...
  • 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.
  • >>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?
  • javascript: is a pseudo-protocol implemented by browsers in the early days of the web.
  • 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
    ===========
  • JavaScript Help
    Can anyone explain why or how can javascript codes are executable in the address bar of a web borwser?
  • 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)
  • 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?
  • If you want to debug in IE, or Safari for that matter, try Firebug lite.
  • Dan
    Thanks Si, these are cool tools!
  • 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
  • Dan
    Firebug is Firefox only! But every serious programmer knows you can't use IE to debug ;)
  • BK
    How do you debug JS in IE using Firebug? Isn't Firebug only for FF?
blog comments powered by Disqus