在iframe中形成POST而不影响历史 [英] form POST in iframe without affecting history
问题描述
是否有可能在iframe内,而不会影响浏览器的历史提交表单?
我实现发送一个跨域POST请求。它使用JavaScript来创建和iframe内提交表单。它的工作原理,但每个请求将项目添加到浏览器的历史记录。
任何人都知道解决的办法?我试着两者的innerHTML和的createElement创建的iframe。我见过没有区别为止。
PS - 我喜欢使用XMLHttt prequest(Ajax的),但它不支持跨域发送数据。我喜欢使用GET而不是职位,但我需要发送超过2K的数据。
下面是我的code一个版本。我试过许多变化,并已找遍了,布提似乎无法找到不影响浏览器的历史记录的解决方案。我相信这是不可能的 - 任何人都可以证实,
? < HTML>
< HEAD>
<脚本类型=文/ JavaScript的>
功能提交(PARAMS){
VAR DIV = document.createElement方法('格');
div.innerHTML ='< IFRAME高度=50WIDTH =50>< / IFRAME>';
document.body.appendChild(DIV);
VAR IFRAME = div.firstChild;
VAR iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
iframeDocument.open();
iframeDocument.close();
VAR形式= iframeDocument.createElement(形式);
iframeDocument.body.appendChild(表);
form.setAttribute('行动','http://some-other-domain.com/submit-here');
form.setAttribute(方法,POST);
对于(参数在PARAMS){
VAR场= iframeDocument.createElement('输入');
field.setAttribute(类型,隐藏);
field.setAttribute(名称,参数);
field.setAttribute('值',则params [参数]);
form.appendChild(场);
}
form.submit();
}
在window.onload =功能(){
的document.getElementById('按钮')。的onclick =函数(){
提交({
X:有些价值
Y:另一种价值,
Z:新的Date()的getTime()。
});
}
}
< / SCRIPT>
< /头>
<身体GT;
< H1>使用Javascript的跨域POST的例子...< / H1>
<输入ID =按钮类型=按钮值=点击发送>
< /身体GT;
< / HTML>
它的工作原理用JS来添加一个IFRAME的SRC托管在您的站点(到第三方托管的脚本需要发送数据的域?)这IFRAME可能包括需要使用Javascript作出XMLHtt prequest到/其领域。至于获取的实际数据,这个IFRAME来自第三方站点 - 尝试: HTTP ://softwareas.com/cross-domain-communication-with-iframes 。这涉及到在IFRAME网址,您可以通过JS的IFRAME内,则读取年底改变片段标识符(#something)一pretty的聪明的解决方案。
另外一种猜测,但如果你钉过去做方案到类似的历史问题(使用location.replace)对上述这种希望应该让你做锚变化的部分,而不破坏历史堆栈。
Is it possible to submit a form inside an iframe without affecting the browser's history?
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.
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 - 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>
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.
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屋!