Reputation: 5821
I know that in Javascript, I can call the function self.close() to close my current window. I was wondering what if rather than closing the window I wanted to alert "foo", is that possible?
which in this case my code would look like
function funky(){
alert("foo");
}
can I do this in my html
self.funky()
To invoke funky? In other words I want to alert my user a message before they close the browser
Upvotes: 2
Views: 132
Reputation: 179086
self
is not special other than the fact that it is initially set to reference window
. Similarly, in the global context this
also refers to window
. Within functions, this
will be defined to be the context of the function, and self
is often used to preserve a context:
function Foo() {
var self;
self = this;
setTimeout(function () {
self.bar(); //here `self` references the Foo instance
}, 1000);
}
Foo.prototype = {
bar: function () {
self.console.log('bar!'); //here `self` references `window`
}
}
new Foo();
Upvotes: 0
Reputation: 413747
The global property "self" is just an alias for window
(in some browsers). Thus any property of window
can be accessed via "self" too.
If you declare a global function, then it too becomes a property of "self"/window
.
Upvotes: 3