Last active
December 14, 2015 04:39
-
-
Save judofyr/5029644 to your computer and use it in GitHub Desktop.
formproxy
This file contains 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
<script>parent.postMessage("Hello world", "*")</script> | |
This file contains 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
<script src="s.js"></script> | |
<body> | |
<form action="foo.html" name="f"> | |
</form> | |
<script> | |
formproxy(f, function(data) { | |
console.log(data); | |
}); | |
</script> | |
</body> | |
This file contains 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
(function() { | |
var id = 0; | |
var pending = []; | |
function createFrame() { | |
var frame = document.createElement('iframe'); | |
frame.src = 'about:blank'; | |
frame.name = '_formproxy' + (formproxy.id++); | |
frame.setAttribute('style', 'position:absolute;width:1px;height:1px;left:-200px'); | |
document.body.appendChild(frame); | |
return frame; | |
} | |
window.addEventListener('message', function(evt) { | |
for (var i = 0; i < pending.length; i++) { | |
var req = pending[i]; | |
if (req.form.contentWindow === evt.otherWindow) { | |
if (req.cb) req.cb(evt.data); | |
req.frame.parentElement.removeChild(req.frame); | |
pending.splice(i, 1); | |
break; | |
} | |
} | |
}, false); | |
window.formproxy = function(form, cb) { | |
var frame = createFrame(); | |
form.target = frame.name; | |
pending.push({form: form, cb: cb, frame: frame}); | |
form.submit(); | |
}; | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment