I set up a thank you page redirection which is working fine. But I want to open the redirected URL in a new window. How can I do that?
Here is the code -
add_action( 'wp_footer', 'redirect_cf7' );
function redirect_cf7() {
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'https://example.com/thank-you/';
}, false );
</script>
}
Please help me to achieve that without any plugin.
For your reference, here is the official documentation: Contact Form 7 DOM Events
Use this may be this will help you out Now the page will open in another window
window.open("https://www.google.com");
You can use window.open
document.addEventListener( 'wpcf7mailsent', function( event ) {
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = window.open('https://example.com/thank-you/', '_blank' );
}, false );
This is local snippet prove that all work(oblivius don't work with snippet but you need to run with local server):
function redirect(){ location = window.open('https://google.com/', '_blank' ); }
<button onclick="redirect()">redirect</button>
Okay, got it working. Here is the code which solved the issue:
<?php
add_action( 'wp_footer', 'redirect_cf7' );
function redirect_cf7() { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
_location = 'https://example.com/thank-you/';
window.open(_location, "MsgWindow");
}, false );
</script>
<?php }
Thanks to all of you for your time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.