是否可以在iframe中提交表单而不影响浏览器的历史记录?
我已经实现了发送跨域POST请求.它使用Javascript在iframe中创建和提交表单.它有效,但每个请求都会在浏览器的历史记录中添加一个项目.
有人知道解决这个问题吗?我尝试用innerHTML和createElement创建iframe.到目前为止,我没有看到任何差异.
PS - 我很想使用XMLHtttpRequest("Ajax"),但它不支持跨域发送数据.我希望使用GET而不是post,但我需要发送超过2k的数据.
这是我的代码的一个版本.我已经尝试了很多变化并且已经搜索过了,但我似乎无法找到一个不影响浏览器历史的解决方案.我相信这是不可能的 - 任何人都可以证实吗?
<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' …
Run Code Online (Sandbox Code Playgroud) 我从当前窗口的片段标识符 ( location.hash
) 获取一个字符串。我想使用该字符串作为 的参数location.replace(str)
。
正常情况下,该字符串将来自我控制的代码,因此我不担心验证该字符串是否为 URL。如果该字符串不是 URL,则替换调用将会失败。没关系。
我担心的是确保该字符串不是javascript:
URL 或其他任何允许某人在我的域上运行任意 Javascript 的内容。目前,我只是检查一下str.indexOf('http') == 0
。
这足够了还是我应该进一步清理这个字符串?
为什么以下代码不起作用?
var f = document.getElementsByTagName;
var x = f('div');
Run Code Online (Sandbox Code Playgroud)
我在Chrome中遇到"TypeError:Illegal invocation",Safari中出现"TypeError:Type error".我没有在Firefox中收到错误,但它不起作用.我还没有在IE或Opera中进行测试.