在网页上运行代码

来源:互联网 发布:速溶咖啡 知乎 编辑:程序博客网 时间:2024/05/01 14:09
          昨天我说到了拷贝文本的问题,这个技巧其实在做编辑器这方面有些很普遍的应用,包括今天将要讨论的‘在网页上运行代码’的问题。这里主要有三个问题:
        1.文本的拷贝,怎样实现跨浏览器,我们已经实现了
        2.新建一个窗口,怎样向里面写数据(操作窗口的一些问题)
        3.怎样另存为(在firefox在依旧没有解决,请高手不吝赐教!)
        
  1. <!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     <meta name="Keywords" content="简单的XHTML页面" />
  6.     <meta name="Description" content="这是一个简单的XHTML页面" />
  7.     <title>简单的XHTML页面</title>
  8.     <script type="text/javascript" language="javascript" >
  9. /*****运行代码*******************************/
  10. function runCode() {
  11. var newWin = window.open(''"_blank"'');
  12. newWin.document.open('text/html''replace');
  13. //newWin.opener = null;
  14. var testCode=document.getElementById("txtTestCode").value;
  15. newWin.document.write(testCode); //向新打开的窗口中写数据
  16. newWin.document.close();   //不关闭没有本质的影响,但浏览器会一直显示加载的样式
  17. }
  18. /*****复制代码到粘贴板*********************/
  19. function copyCode(obj){    
  20.     var testCode=document.getElementById("txtTestCode").value;    
  21.     if(copy2Clipboard(testCode)!=false)
  22.     {        
  23.         alert("生成的代码已经复制到粘贴板,你可以使用Ctrl+V 贴到需要的地方去了哦!  ");        
  24.     }    
  25. }
  26. //很大的一陀是为了对firefox的兼容
  27. copy2Clipboard=function (txt){    
  28.     if(window.clipboardData){        
  29.         window.clipboardData.clearData();        
  30.         window.clipboardData.setData("Text",txt);        
  31.     }else if(navigator.userAgent.indexOf("Opera")!=-1){        
  32.         window.location=txt;                
  33.     }else if(window.netscape){        
  34.         try{            
  35.             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");            
  36.         }catch(e){            
  37.             alert("您的firefox安全限制限制您进行剪贴板操作,请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试");            
  38.             return false;            
  39.         }
  40.         var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);        
  41.         if(!clip)
  42.         return ;        
  43.         var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);        
  44.         if(!trans)
  45.         return ;        
  46.         trans.addDataFlavor('text/unicode');        
  47.         var str=new Object();        
  48.         var len=new Object();        
  49.         var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);        
  50.         var copytext=txt;        
  51.         str.data=copytext;        
  52.         trans.setTransferData("text/unicode",str,copytext.length*2);        
  53.         var clipid=Components.interfaces.nsIClipboard;        
  54.         if(!clip)
  55.         return false;        
  56.         clip.setData(trans,null,clipid.kGlobalClipboard);        
  57. }
  58. }
  59. /*****保存代码为html页面,非常遗憾的现阶段只支持IE******/
  60. function saveCode(obj) {
  61. var newWin = window.open('''_blank''top=10000');//这里是个技巧,弹出的页面,虽然去不掉,但是我们可以让它向下移动到屏幕之外
  62. newWin.document.open('text/html''replace');
  63. var testCode=document.getElementById("txtTestCode").value;
  64. newWin.document.write(testCode);
  65. newWin.document.execCommand('saveas','','code.htm');//firefox不兼容的主要原因就是因为ff不支持execCommand('saveas','','filename');
  66. newWin.close();
  67. }
  68. </script>
  69. </head>
  70. <body>
  71.  <textarea id="txtTestCode" rows="12" cols="75">
  72.  <!doctype html public "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  73. <html xmlns="http://www.w3.org/1999/xhtml">
  74. <head>
  75. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  76. <meta name="Keywords" content="YES!B/S!" />
  77. <meta name="Description" content="This page is from http://Justinyoung.cnblogs.com" />
  78. <title>CSS/Javascript demo</title>
  79. </head>
  80. <body>
  81. <p>A testpage from<a href="http://justinyoung.cnblogs.com/" title="博客园Yes!B/S!博客">YES!B/S!</a></p>
  82. </body>
  83. </html>
  84.  </textarea>
  85.  <div style="clear:both;">
  86. <INPUT onclick="runCode()" type=button value=运行代码> 
  87. <INPUT onclick="copyCode()" type=button value=复制代码> <INPUT onclick=saveCode() type=button value=另存代码>
  88. </div>
  89. </body>
  90. </html>