window.open(url)上的流星回调无法打开窗口

时间:2013-09-02 17:04:41

标签: javascript url callback window meteor

如果有人知道window.open()Meteor.call()的回调中无法解决原因,我会很高兴。您可以通过在客户端上window.open(url)的回调中调用Meteor.call('method',argument,callback(e,r){...})来轻松地重现此问题。在回调之外它可以工作,并且在回调中,window.location = url正确地重定向。

我的数据库中有一些来自filepicker.io的安全网址。由于提前生成所有策略和签名效率低下,我想在有人实际尝试检索这些文件时在click事件中生成它们。不幸的是,在Meteor.call('methodname',param,callback(e,r){...})的客户回调中,window.open(url)似乎不起作用,我无能为力。

模板

<template name="upload">
  <div class="btn-group btn-group-vertical">
    {{#each files}}
      <button id="fp" class="btn btn-primary">{{filename}}</button>
    {{/each}}
  </div>
</template>

的客户机/ client.js

Template.upload.files=function(){
    return files.find({});
}
Template.upload.events({
  'click #fp':function(){
    // window.open(this.url)
    // if I uncomment the above line, a new window
    // opens with the unsigned url
    // (this.url is a valid mongo cursor)
    Meteor.call('signedUrl',this.url,function(err,result){ // result is signed url
      console.log(result); // loggs the correct url in the console
      // window.location = result;
      // if uncommented, the line above redirects correctly 
      window.open(result); // does NOT open the new window with the signed url
    });
  }
});

服务器/ server.js

Meteor.methods({
  signedUrl: function(url) {
    // some proven-to-work-code that you can find at
    // http://stackoverflow.com/questions/18546676
    console.log(signed_url); // loggs the correctly signed url on the server
    return signed_url;
  }
});

提前感谢您的任何提示!最好的问候

1 个答案:

答案 0 :(得分:1)

这可能是由chrome / safari的反弹出过滤器捕获的。

您可能希望以非恶意方式使用新窗口,但现代浏览器需要用户输入才能打开新窗口。在回调中没有用户输入“触发器”,因此浏览器可能认为它是一个弹出/广告。

真的没有太多方法可以通过这个。您可以在运行回调时创建一个新按钮,然后让用户单击它以打开一个新窗口。

相关问题