Reputation: 43265
This is the sample code:
in the pop up :
<body onunload='stopThisAndChangeParentInstead()' >
<a href='MY_URL'> Click here </a>
When the user clicks "Click here", the unload event fires which changes the URL of the parent and closes this popup window.
function stopThisAndChangeParentInstead()
{
window.opener.top.location.href = "MY_URL";
window.close();
}
My questions:
Upvotes: 0
Views: 4039
Reputation: 26183
I'd suggest using the target
attribute on the link inside the popup to make the url open in the parent window (target="_parent"
or target="_top"
ought to work) and then use javascript only for closing the popup. it's a cleaner solution in my opinion.
Upvotes: 3
Reputation: 5579
.
<a id="myLink" href="http://myurl">
...
function stopThisAndChangeParentInstead()
{
var url = document.getElementById('myLink').href;
window.opener.top.location.href = url;
window.close();
}
Upvotes: 1
Reputation: 6094
instead of using an unload event, i'd recommend attaching an onclick event to the anchor tags directly. using jquery this would be:
$('a').click(function(){
window.opener.top.location.href = $(this).attr('href');
window.close();
return false;
});
although it's a somewhat, say, unusual way to set up navigation this way, i can't see any problems arising from this (aside from confusing the user, maybe)
Upvotes: 1