表单在iframe中POST而不影响历史记录 [英] form POST in iframe without affecting history
问题描述
是否可以在iframe中提交表单而不影响浏览器的历史记录?
Is it possible to submit a form inside an iframe without affecting the browser's history?
我已实现发送跨域POST请求。它使用Javascript在iframe中创建和提交表单。它工作,但每个请求添加一个项目到浏览器的历史。
I've implemented sending a cross domain POST request. It uses Javascript to create and submit a form inside an iframe. It works, but each request adds an item to the browser's history.
任何人都知道这个方法吗?我试过创建iframe与innerHTML和createElement。我到目前为止没有见过。
Anyone know a way around this? I've tried creating the iframe with both innerHTML and createElement. I've seen no difference so far.
PS - 我想使用XMLHtttpRequest(Ajax),但它不支持跨域发送数据。我想使用GET而不是post,但是我需要发送超过2k的数据。
PS - I would love to use XMLHtttpRequest ("Ajax"), but it doesn't support sending data across domains. And I would love to use GET instead of post, but I need to send more than 2k of data.
这里是我的代码的一个版本。我试过很多变化,并搜索了所有,但我似乎找不到一个不影响浏览器的历史的解决方案。我相信这是不可能的 - 任何人都可以确认吗?
Here's one version of my code. I've tried many variations and have searched all over, butI can't seem to find a solution that doesn't affect the browser's history. I believe it's not possible -- can anyone confirm that?
<html>
<head>
<script type="text/javascript">
function submit(params) {
var div = document.createElement('div');
div.innerHTML = '<iframe height="50" width="50"></iframe>';
document.body.appendChild(div);
var iframe = div.firstChild;
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
iframeDocument.open();
iframeDocument.close();
var form = iframeDocument.createElement('form');
iframeDocument.body.appendChild(form);
form.setAttribute('action', 'http://some-other-domain.com/submit-here');
form.setAttribute('method', 'POST');
for (param in params) {
var field = iframeDocument.createElement('input');
field.setAttribute('type', 'hidden');
field.setAttribute('name', param);
field.setAttribute('value', params[param]);
form.appendChild(field);
}
form.submit();
}
window.onload = function() {
document.getElementById('button').onclick = function() {
submit({
'x' : 'Some Value',
'y' : 'Another Value',
'z' : new Date().getTime()
});
}
}
</script>
</head>
<body>
<h1>Example of using Javascript to POST across domains...</h1>
<input id="button" type="button" value="click to send">
</body>
</html>
推荐答案
使用JS添加IFRAME src托管在您的站点(第三方托管脚本需要发送数据的域)。此IFRAME可以包括需要的Javascript,以使XMLHttpRequest到您的/它的域。至于从第三方网站获取此IFRAME的实际数据 - 请尝试: http://softwareas.com/cross-domain-communication-with-iframes 。这是一个非常聪明的解决方案,涉及更改IFRAME URL末尾的片段标识符(#something),然后您可以通过IFRAME中的IFRAME读取。
Does it work to use JS to add an IFRAME whose src is hosted at your site (the domain to which the third-party-hosted script needs to send data?) This IFRAME could include the needed Javascript to make an XMLHttpRequest to your/its domain. And as for getting the actual data to this IFRAME from the third-party-site - try: http://softwareas.com/cross-domain-communication-with-iframes . It's a pretty clever solution involving changing fragment identifiers (#something) at the end of the IFRAME URL, which you can then read via JS within the IFRAME.
,但如果您将这个过去的SO解决方案绑定到类似的历史问题(使用location.replace)上面这希望应该让你做锚定改变部分,而不中断历史堆栈。
Also a guess, but if you tack this past SO solution to a similar history problem (using location.replace) on to the above this hopefully should let you do the anchor-changing part without disrupting the history stack.
这篇关于表单在iframe中POST而不影响历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!