Created
August 9, 2013 07:42
-
-
Save nnarhinen/6191824 to your computer and use it in GitHub Desktop.
POST Form to new window and wait for postMessage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Post form in a new window without losing handle to the window</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script> | |
$(function() { | |
$('form').on('submit', function(ev) { | |
var form = $(this); | |
form.attr('target', 'new-window'); | |
var win = window.open('about:blank', 'new-window'); | |
window.addEventListener('message', function(msg) { | |
console.log(msg); | |
win.close(); | |
}, false); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1></h1> | |
<form action="post.php" method="POST"> | |
<p> | |
<label for="fe-text">Text</label><br /> | |
<input type="text" id="fe-text" name="my_text"> | |
</p> | |
<p> | |
<button type="submit">Submit</button> | |
</p> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Post form result</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script>window.postArgument = "<?php echo $_POST['my_text'];?>";</script> | |
<script> | |
$(function() { | |
window.opener.postMessage(window.postArgument, '*'); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Post result</h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment