2014-12-04 48 views
0

为了让我打印预览“接受”的风格,我需要,包括我的弹出窗口,我使用的打印中的一个函数:如何包括window.open内的jQuery函数

功能我使用的打印是:

$('#printPersonnel').click(function() { 
      var data = document.getElementById('personnelTable').outerHTML; 
      var mywindow = window.open('', data); 
      mywindow.document.write('<html><head><title>NORA Loading</title>'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Content/Site.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/examples-offline.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/kendo.rtl.min.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/kendo.common.min.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/kendo.dataviz.default.min.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/kendo.dataviz.min.css">'); 
      mywindow.document.write('<link rel="stylesheet" type="text/css" href="http://localhost:49573/Scripts/kendo.default.min.css">'); 

      mywindow.document.write('</head><body>'); 
      mywindow.document.write(data); 
      mywindow.document.write('</body></html>'); 

      mywindow.document.close(); 
      mywindow.focus(); 
      mywindow.print(); 
      mywindow.close(); 
      return true; 
     }); 

不过,我需要包括以正确添加此格式(我使用Telerik的UI格)一$(document).ready功能。

这意味着,我需要包括我的文档中的脚本标记。

我的问题在于试图添加结束脚本标签(它把自己看作父页面的一部分,而不是内部的功能)。

任何建议,我怎么能实现这个功能?


我想某个表的标签,这是在文件准备在页面加载看作是一个Telerik的网格格式化内打印一切


编辑

我的问题是与mywindow脚本的结束标记:

mywindow.document.write('});'); 
mywindow.document.write('</script>'); //issue with this line/seen as esc sequence (i think) 

enter image description here

+0

你能提供包括''”);'工作,可能是问题在于里面'.ready'函数体,语法错误还是什么? – Zudwa 2014-12-04 12:57:26

回答

1

你可以用反斜杠与最终的脚本。

mywindow.document.write("<script>(function(){alert('1');})();<\/script>"); 
1

您可以分割成</script>部分,所以它不会被视为结束标签:

mywindow.document.write("<script>(function(){alert('1');})();" + "<" + "/script>"); 
+1

IIRC,这应该解决它。 ***更新*** http://stackoverflow.com/a/236106/1414562 – 2014-12-04 13:28:13

+0

@ A.沃尔夫你是对的。编码'<'甚至是更好的方法(恕我直言)。 – Zudwa 2014-12-04 13:37:48