user2234760
user2234760

Reputation: 143

How do I redirect the window that opened a popup window to a new page by clicking a button in the popup window?

I have a page that opens a popup window and in the popup window, I have a link that should redirect window that opened the current window to a page called privacy.html.

I know I'm supposed to use window.opener to refer to that window and I found this other question where the solution was to use setInterval. That might be useful for this.

How to focus on a previously opened window using window.open()

Anyways this is my code and it doesn't work to redirect the opener window but you can see what I'm trying to do.

<script type="text/javascript">


function validateEmail() {
    var emailRule =  /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    if (emailRule.test(document.forms[0].email.value)) {
         document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
         setTimeout("window.close()", 3000);
    }
    else {
         document.getElementById("message").innerHTML = "Please enter a valid email address";
    }
}

function privacyWindow() {
     window.opener("privacy.html");
}
</script>

</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<p id="message"><br></p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="button" value="Sign up" onclick="validateEmail();"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="" onclick="privacyWindow();">Privacy Policy</a></p>
</body>
</html>

Upvotes: 0

Views: 4062

Answers (1)

Ian
Ian

Reputation: 50905

Change:

window.opener("privacy.html");

To:

window.opener.location.href = "privacy.html";

Upvotes: 2

Related Questions