javascript – 打开一个包含ASPX回发结果的弹出窗口

前端之家收集整理的这篇文章主要介绍了javascript – 打开一个包含ASPX回发结果的弹出窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个ASPX页面,其中包含许多字段,当我单击“导出到PDF”按钮时,它会生成PDF文档.

我现在想在JavaScript中使用“打印PDF”按钮,它可以执行以下操作:

  1. w = window.open(?);
  2. w.print();
  3. w.close();

哪里“?”将执行与“导出到PDF”按钮相同的回发.

解决方法

如果您需要将表单提交(回发)到新窗口,您可以尝试将表单目标更改为假,例如:
  1. var form = $("form");
  2. form.attr("target","__foo");

提交表格.

  1. form.submit();

删除目标(setitmeout(,1) – 在js“event-queue”结尾处的事件,在我们的例子中 – 在表单提交后):

  1. setTimeout(function () { form.removeAttr("target"); },1);

此外,在提交之前,您可以尝试打开带有__foo id的窗口以获得更多样式,并且表单将在此窗口中提交(回发)而不是新窗口:

  1. var wnd = window.open('','__foo','width=450,height=300,status=yes,resizable=yes,scrollbars=yes');

但我不知道如何处理提交的窗口并捕获onload或jquery的ready事件.如果你能分享解决方法,请拨打wnd.print();你可以在这个wnd中使用iframe,也许你会找到一个解决方案.

更新:

试着看看这个原型[在Chrome中测试]:

  1. <!DOCTYPE html>
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
  7. <script type="text/javascript">
  8. function PrintResult() {
  9. var wnd,checker,debug;
  10.  
  11. debug = true;
  12.  
  13. // create popup window
  14. wnd = window.open('about:blank','width=700,height=500,scrollbars=yes');
  15.  
  16. // create "watermark" __loading.
  17. wnd.document.write("<h1 id='__loading'>Loading...</h1>");
  18.  
  19. // submit form to popup window
  20. $("form").attr("target","__foo");
  21. setTimeout(function() { $("form").removeAttr("target"); },1);
  22.  
  23. if (debug)
  24. {
  25. $("#log").remove();
  26. $("body").append($("<div id='log'/>"));
  27. }
  28.  
  29. // check for watermark
  30. checker =
  31. setInterval(function () {
  32. if (debug) $("#log").append('. ');
  33.  
  34. try {
  35.  
  36. if (wnd.closed) { clearInterval(checker); return; }
  37.  
  38. // if watermark is gone
  39. if (wnd.document == undefined || wnd.document.getElementById("__loading") == undefined) {
  40. if (debug) $("#log").append(' printing.');
  41. //stop checker
  42. clearInterval(checker);
  43.  
  44. // print the document
  45. setTimeout(function() {
  46. wnd.print();
  47. wnd.close();
  48. },100);
  49. }
  50. } catch (e) {
  51. // ooops...
  52. clearInterval(checker);
  53. if (debug) $("#log").append(e);
  54. }
  55. },10);
  56. }
  57. </script>
  58. </head>
  59. <body>
  60. <form id="form1" runat="server">
  61. <div>
  62. <asp:Button runat="server" ID="ReportButton" OnClick="ReportRenderClick" Text="Export to PDF" OnClientClick="PrintResult()"/>
  63. <asp:Button runat="server" Text="Just a button."/>
  64. </div>
  65. </form>
  66. </body>
  67. </html>

这是.cs文件

  1. public partial class Default : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender,EventArgs e)
  4. {
  5.  
  6. }
  7.  
  8. protected void ReportRenderClick(object sender,EventArgs e)
  9. {
  10. Response.Clear();
  11. Thread.Sleep(2000);
  12. Response.ContentType = "application/pdf";
  13. Response.WriteFile("d:\\1.pdf");
  14.  
  15. //Response.ContentType = "image/jpeg";
  16. //Response.WriteFile("d:\\1.jpg");
  17.  
  18. //Response.Write("Hello!");
  19. Response.End();
  20. }
  21. }

猜你在找的JavaScript相关文章