Dennis Dennis - 3 years ago 127
Javascript Question

How to open all window.open onclick functions in parent

In my project there are several div elements that contain onclick events like so:

<div id='main'>

<div onclick='window.open("/folder/file.php", "_self")'>
Content
</div>

<div onclick='window.open("/folder/file.php", "_self")'>
Content
</div>

<div onclick='window.open("/folder/file.php", "_self")'>
Content
</div>

<div onclick='window.open("/folder/file.php", "_self")'>
Content
</div>
</div>


The issue i have now is, that this page can be opened in the normal browser window and also within a popup. If this page is opened within the popup i would like all of those onclick links to be opened within the parent window.
Is there a way to accomplish that with javascript or maybe even with jquery?

Answer Source

Define your own function to open the URL, and have it check whether the window is a popup.

function my_open(url) {
    if (window.opener) { // We're in a popup
        window.opener.location = url;
    } else {
        window.location = url;
    }
}

Then use onclick='my_open("/folder/file.php")'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download