views:

266

answers:

2

How to add an onLoad function to an opened window? (target is "self", not window.open, just a regular link)

Is it possible with onbeforeunload or something? Or add onclick to the "normal link"?

I mean like this:

<a href="page2.htm" onclick="theWindowIHaveOpened.onLoad = function(){alert('loaded')}">...
+1  A: 

No, you will have no control over the next page with a basic http call of a link.

epascarello
A: 

With an onclick handler on the link and an external chunk of script, you could do something like:

<script type="text/javascript">
function loadHandler()
{
    // do stuff
}

function clickHandler(elem)
{
    h = window.open();
    h.onload = loadHandler;
    h.location.href = elem.getAttribute('href');
    return false;
}
</script>

<a href="page2.htm" onclick="return clickHandler(this);">Linky</a>

Though of course you may need to do some work with that to get it working cross-browser.

Edit Added the return statements so that normal navigation of the <a> tag is suppressed.

Matt Sach
Question states no window.open
epascarello
Oh yes, whoops. Sorry, ignore my idea then! Yes, with no use of window.open() you cannot get a reference to the new window in order to manipulate it. Is there a particular reason for the fact that you can crate an onclick handler, but cannot use window.open()?
Matt Sach
(that last bit of the comment to Jani, of course)
Matt Sach
no, window.open in this way is cool :)
János Harsányi
only the "target" is the problem...i need to open the window to "_self" and this opens the page in a new window.
János Harsányi
So you need the link to load in the current window, but set up something to happen when the page it points to finishes loading? Do you have control over the page it points to? If not, then I'm afraid there's nothing you can do. If you do have control over it, let me know and I'll edit my original answer with a possible solution.
Matt Sach