How can I actually run a JavaScript function?

When trying to understand how the web server works, I came across 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 am not a JavaScript expert, so I don’t understand how to work with an empty function. Somebody knows?

Thank.

+3
source share
6 answers

This is a makeshift substitute voidto avoid an expression returning a value.

window.open will return a link to an open window, and this may lead to unexpected results.

For example, try pasting javascript:a=1in the address field - this will lead to a blank screen with the number 1 in it, because the default browser will try to use the result of any expression that starts as a new document.

, javascript:void(a=1), void , .

donothing(foo=bar) Function.prototype(foo=bar) , void .

, void , , , javscript: ( ).

+19

, , open(...). ( Javascript ).

+3

donothing , . .

+2

donothing . .

-, - , javascript: . .

+1

, donothing, ... open . , , open .

donothing , :)

0

, void , , javscript: ( ).

Not true 100%. If you write in html, <a href="javascript:1+2;">clickme</a>many browsers will still give a blank screen with a return value that evaluates to true in Javascript. The exact reason to use the void operator in tags is <a>also when, for example, you automatically generate tags in a template language.

0
source

Source: https://habr.com/ru/post/1746175/


All Articles