I am trying to open an alert box when a window loads. Here is what I have:
var imawindow = window.open("http://google.com");
imawindow.onload() = function () {
alert("hey");
}
What am I doing wrong? Thanks in advance!
I am trying to open an alert box when a window loads. Here is what I have:
var imawindow = window.open("http://google.com");
imawindow.onload() = function () {
alert("hey");
}
What am I doing wrong? Thanks in advance!
You shouldn't execute it before you assign. Remove the ()
. If you wanna call the alert
on the caller, then use window.opener
:
imawindow.onload = function () {
window.opener.alert("hey");
}
And this should respect the same domain policy. The calling window, should be a relative page in your domain. Not something like you can call http://www.google.com/
from http://www.facebook.com/
. But you can call it from index.html
on hello.html
, provided that both index.html
and hello.html
are in from the same domain, say http://example.com/index.html
and http://example.com/hello.html
I would like mention that it will not work on cross-domain.
If you are opening a new page within the same domain than you will be able to achieve this by:
var imawindow = window.open("samedomainpage.html");
imawindow .addEventListener('load', functionToCall, false);
For cross-domain this will not work for security reasons. Read more about it here