Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将参数传递到BLOB对象URL_Javascript_Html_Blob_Parameter Passing - Fatal编程技术网

Javascript 将参数传递到BLOB对象URL

Javascript 将参数传递到BLOB对象URL,javascript,html,blob,parameter-passing,Javascript,Html,Blob,Parameter Passing,假设我有一个对html文件的引用,作为Blobb,我为它创建了一个URL,URL=URL.createObjectURL(b) 这给了我一些类似于blob:http%3A//example.com/a0440b61-4850-4568-b6d1-329bae4a3276 然后,我尝试使用GET参数?foo=bar在一个中打开它,但它不起作用。如何传递参数 var html ='<html><head><title>Foo</title></h

假设我有一个对html文件的引用,作为Blob
b
,我为它创建了一个URL,
URL=URL.createObjectURL(b)

这给了我一些类似于
blob:http%3A//example.com/a0440b61-4850-4568-b6d1-329bae4a3276

然后,我尝试使用GET参数
?foo=bar
在一个
中打开它,但它不起作用。如何传递参数

var html ='<html><head><title>Foo</title></head><body><script>document.body.textContent = window.location.search<\/script></body></html>',
    b = new Blob([html], {type: 'text/html'}),
    url = URL.createObjectURL(b),
    ifrm = document.createElement('iframe');
ifrm.src = url + '?foo=bar';
document.body.appendChild(ifrm);

// expect to see ?foo=bar in <iframe>
var html='Foodocument.body.textContent=window.location.search',
b=新的Blob([html],{type:'text/html'}),
url=url.createObjectURL(b),
ifrm=document.createElement('iframe');
ifrm.src=url+'?foo=bar';
文件.body.appendChild(ifrm);
//希望看到?foo=bar in

我认为向url添加查询字符串不会起作用,因为它本质上会将其更改为不同的url。
但是,如果您只是想传递参数,您可以使用散列向url添加一个片段

ifrm.src = url + '#foo=bar';

为完整起见,如果您希望能够引用带有问号“查询字符串”指示符的blob,您可以在Firefox中选择任何方式,例如:
blob:lalalal?thisworksinfirefox

对于Chrome,上面的操作将不起作用,但这将:
blob:lalalla#?这将在RomeandFirefox中工作

对于Safari和Microsaft,没有什么真正起作用,所以做一个这样的预测试,然后相应地计划:

 function initScriptMode() {
  var file = new Blob(["test"], {type: "text/javascript"});
  var url = URL.createObjectURL(file) + "#test?test";

   var request = new XMLHttpRequest();
    request.responseType = responseType || "text";
    request.open('GET', url);


    request.onload = function() {
        alert("you can use query strings")
    };

    try {
        request.send(); 
    }
    catch(e) {
        alert("you can not use query strings")
    }
}

如果使用Javascript Blob(例如WebWorker)执行此操作,则只需将参数作为全局变量添加到Blob构造函数中即可:

const parameters = 'parameters = ' + JSON.stringify({foo:'bar'});
const body = response.body; // From some previous HTTP request
const blob = new Blob([parameters, body], { type: 'application/javascript' });
new Worker(URL.createObjectURL(blob));
或者更一般的情况是只在location对象上存储原始URL

const location = 'location.originalHref = "' + url + '";';
const body = response.body; // From some previous HTTP request
const blob = new Blob([location, body], { type: 'application/javascript' });
new Worker(URL.createObjectURL(blob));

如果您可以将它们作为属性添加到根
标记中,或者将
元素用于url,或者将它们作为脚本标记插入,您也可以使用HTML来执行此操作,但这需要您修改响应HTML,而不只是预先添加一些额外的数据

这样做的目的是什么?正是我所说的:传递一个参数传递一个参数来实现什么?我已经把你的问题写在我是如何理解它的;希望你不会再次被否决——如果我的理解是错误的,请回过头来,这在Safari中不起作用。Safari有什么解决方案吗?