I have the following script from here:
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
download('test.txt', 'Hello world!');
Now I don´t understand the usage of if (document.createEvent)
:
I read the answer for the question In JavaScript, does it make a difference if I call a function with parentheses? and read some other questions.
In most examples is some kind of referencing like window.onload = initAll;
or var ret = Multiply;
but thats obviously is not the case here. The most interesting example for me was:
function Multiply(operator, operand) {
return operator * operand;
}
var operator = 3;
var operand = 4;
var ret = Multiply;
Here, according to the author, Multiply
does not get executed and ret
reference to the function Multiply
.
Also here it is stated that calling a function without parentheses is a reference.
But thats get me confused even more, because for me a reference to document.createEvent
, which is a function to create an event, does not make sense.
Documentations about the function document.createEvent()
like here does not point out the usage without parameters.
So, please bring some light to the darkness of my brain. I appreciate your help.
Questions (short):
- Is
document.createEvent
a function call or a reference? - What does
document.createEvent
return?