2011-03-30 76 views
1

我有一个允许用户选择支票的表单,当提交时,会创建一个PDF,并在新的浏览器选项卡中打开。它没有任何品牌,并且可能会在插件中打开,所以我不希望它接管我的网站的选项卡。所以我将表格的target设置为_blank你可以选择浏览器目标服务器端吗?

但是用户可能提交没有足够信息来创建PDF的表单,在这种情况下,我将标记错误(服务器端)并重新呈现表单。但是因为我设置了窗体的目标,所以这个重新渲染也在一个新的标签中打开,这就是而不是我想要的 - 在这种情况下,我希望它的行为好像target_top

所以问题是:我可以更改浏览器的渲染目标服务器端吗?

是的,我知道这可以用客户端JavaScript来完成,但是JS让我很恼火,而且我必须做验证服务器端。我可能最终不得不使用它,但请不要将其作为答案 - 我更加好奇,如果我正在尝试什么,甚至可以完成。

PS:我在Ruby on Rails 2.3.8上,以防有人知道框架特定的解决方案。

回答

0

不。这是纯粹的客户端特定功能。事实上,获得只支持一个窗口的浏览器和target属性根本无效的浏览器是完全可能的。甚至有人努力使该属性完全从未来的HTML标准中消失(例如,XHTML分支没有这样的属性)。

我能想到的HTML和HTTP之间唯一的重叠是<meta http-equiv>标签(其中HTML会影响HTTP控制的行为)。 HTTP是一种传输协议,旨在处理任何类型的数据。让它控制呈现将是一个非常可怕的担忧组合。

幸运的是,我们生活在一个支持JavaScript的世界。使用AJAX请求验证表单相当容易,特别是对于像jQuery这样的库。

例如,该脚本对URL执行POST请求(在这种情况下,/pdf/validate),并期望页面发回“ok”(如果一切正常)或其他情况下发生错误。

<form method="post" action="/pdf/send" id="pdf-form"> 
    <!-- form stuff here --> 
</form> 

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
    // set to true if we are to bypass the check 
    // this will happen once we've confirmed the parameters are okay 
    var programmaticSubmit = false; 

    // attach an event handler for when the form is submitted 
    // this allows us to perform our own checks beforehand; we'll do so by 
    // cancelling the event the user triggered, and do the submit ourselves if 
    // we detect no error 
    $('#pdf-form').submit(function(event) 
    { 
     if (!programmaticSubmit) 
     { 
      // first off, cancel the event 
      event.preventDefault(); 

      // do an AJAX request to /pdf/validate 
      $.ajax("/pdf/validate", { 
       type: "POST", 
       data: $(this).serialize(), // send the form data as POST data 
       success: function(result) 
       { 
        // this gets called if the HTTP request did not end 
        // abnormally (i.e. no 4xx or 5xx status); 
        // you may also want to specify an "error" function to 
        // handle such cases 
        if (result == "ok") 
        { 
         // since the server says the data is okay, we trigger 
         // the event again by ourselves, but bypassing the 
         // checks this time 
         programmaticSubmit = true; 
         $(this).submit(); 
        } 
        else // something went wrong! somehow display the error 
         alert(result); 
       } 
      }); 
     } 
    }); 
}); 
</script> 
+0

那就是JavaScript了。我终于找到了一个涉及[Window-Target](http.http-stats.com/Window-Target)HTTP头的文件,但文档实际上并不存在,而Firefox忽略了它......我可能会最终使用非常类似于JS的东西 - 谢谢! – 2011-03-31 00:43:43

+0

@Xavier Holt从来没有听说过那个!必须是另一个MS特定的未公开扩展。 – zneak 2011-03-31 02:15:39

+0

我还没有 - 我在看到你提到的那些''标签时偶然发现了它。还有另外一个信誉良好的来源提到它[这里](http://vancouver-webpages.com/META/metatags.detail.html),但他们只能说:“指定命名窗口当前页面;可用于停止出现在包含许多(不是全部)浏览器的框架中的页面。“不知道什么实际上支持(支持?)它... – 2011-03-31 03:17:50

1

对这个问题的解决方法是使用上的PDF内容处置头,以迫使该文件进行下载,避免整个“目标”的方法..

Content-type: application/pdf 

Content-Disposition: attachment; filename="downloaded.pdf" 
+0

我希望我能做到,但那些做我的需求清单的人爱上了他的Adobe Acrobat插件(不寒而栗)... Upvote为理想的答案,但它不适用于我。干杯! – 2011-03-31 00:09:34

相关问题