How can empty JavaScript function actually do something?

javascript

Solution

This is a homemade `void` substitute to avoid having the expression return a value.

`window.open` will return a reference to the opened window, and this can have unexpected results.

For instance, try pasting `javascript:a=1` into the address field - this will result in a blank screen with the number 1 in it as the browser will by default try to use the result of any expression run as the new document.

To avoid this you use `javascript:void(a=1)` as void will not return anything, and so the result isn't used as the new document.

Using `donothing(foo=bar)` or the equivalent `Function.prototype(foo=bar)` is not needed as the built-in `void` does the exact same.

But mind, the use of void is only needed when copying text into the address field, its not necessary when you use the pseudo protocol `javscript:` in links (which you should never do anyway).

Problem

While trying to understand how a web server worked, I came accross this: ``` //myfile.js function donothing(){}; //myfile.html javascript:donothing(open('http://www.acme.com/whatever.jpg','','left=100, right=0, top=100, scrollbars=no, status=no, titlebar=no, resizable=no, toolbar=no, menubar=no, width=255, height=255')) ``` I'm no JavaScript expert, so I don't get how an empty function can be made to work. Does someone know? Thank you.

Original source