tags:

views:

136

answers:

5

Hello

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.

+2  A: 

The donothing function is passed a parameter that it ignores. It is the parameter itself that does the work, however.

Marcelo Cantos
Actually, in order to pass the parameter to donothing, it is first executed. The RESULT (return value) from the function (in this case open) is then passed to donothing.
Konerak
That's kind of what I meant.
Marcelo Cantos
+3  A: 

The one that does the work is open(...). The operands are evaluated first before the function is called (and Javascript doesn't care about the number of operands to the function).

Chris Henry
A: 

Well, although it is difficult to understand the point of donothing without seeing the rest of the code... the open function will be evaluated anyway. So effectively what is happening is that the open function is being called.

Why they're using donothing to do it is difficult to say without other info :)

nico
Thanks everyone for the feedback. The donothing() is located in a .js file, and is used in hyperlinks such as: <a href="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=242, height=325'))"><img src='http://www.acme.com/whatever.jpg'>Click me</a>
OverTheRainbow
What's even weirder, is that the Fiddler proxy doesn't display anything when I click on this type of hyperlink :-/
OverTheRainbow
Its not strange if the image is served from the cache. Mind selecting one of the answers as the solution?
Sean Kinsey
Ah, didn't think of this. Thanks for the tip. Done.
OverTheRainbow
+1  A: 

the call to donothing is just acting as a shroud. The open function is being called before donothing.

Apparently whomever wrote it felt the naked javascript: call was vulnerable. Strange.

Sky Sanders
+15  A: 

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).

Sean Kinsey
+1: Enjoy your first Nice Answer badge :)
Daniel Vassallo