Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
Created August 9, 2013 07:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nnarhinen/6191824 to your computer and use it in GitHub Desktop.
Save nnarhinen/6191824 to your computer and use it in GitHub Desktop.
POST Form to new window and wait for postMessage
<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>
<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