在网页上运行代码
来源:互联网 发布:速溶咖啡 知乎 编辑:程序博客网 时间:2024/05/01 14:09
昨天我说到了拷贝文本的问题,这个技巧其实在做编辑器这方面有些很普遍的应用,包括今天将要讨论的‘在网页上运行代码’的问题。这里主要有三个问题:
1.文本的拷贝,怎样实现跨浏览器,我们已经实现了
2.新建一个窗口,怎样向里面写数据(操作窗口的一些问题)
3.怎样另存为(在firefox在依旧没有解决,请高手不吝赐教!)
- <!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="Keywords" content="简单的XHTML页面" />
- <meta name="Description" content="这是一个简单的XHTML页面" />
- <title>简单的XHTML页面</title>
- <script type="text/javascript" language="javascript" >
- /*****运行代码*******************************/
- function runCode() {
- var newWin = window.open('', "_blank", '');
- newWin.document.open('text/html', 'replace');
- //newWin.opener = null;
- var testCode=document.getElementById("txtTestCode").value;
- newWin.document.write(testCode); //向新打开的窗口中写数据
- newWin.document.close(); //不关闭没有本质的影响,但浏览器会一直显示加载的样式
- }
- /*****复制代码到粘贴板*********************/
- function copyCode(obj){
- var testCode=document.getElementById("txtTestCode").value;
- if(copy2Clipboard(testCode)!=false)
- {
- alert("生成的代码已经复制到粘贴板,你可以使用Ctrl+V 贴到需要的地方去了哦! ");
- }
- }
- //很大的一陀是为了对firefox的兼容
- copy2Clipboard=function (txt){
- if(window.clipboardData){
- window.clipboardData.clearData();
- window.clipboardData.setData("Text",txt);
- }else if(navigator.userAgent.indexOf("Opera")!=-1){
- window.location=txt;
- }else if(window.netscape){
- try{
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- }catch(e){
- alert("您的firefox安全限制限制您进行剪贴板操作,请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试");
- return false;
- }
- var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
- if(!clip)
- return ;
- var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
- if(!trans)
- return ;
- trans.addDataFlavor('text/unicode');
- var str=new Object();
- var len=new Object();
- var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
- var copytext=txt;
- str.data=copytext;
- trans.setTransferData("text/unicode",str,copytext.length*2);
- var clipid=Components.interfaces.nsIClipboard;
- if(!clip)
- return false;
- clip.setData(trans,null,clipid.kGlobalClipboard);
- }
- }
- /*****保存代码为html页面,非常遗憾的现阶段只支持IE******/
- function saveCode(obj) {
- var newWin = window.open('', '_blank', 'top=10000');//这里是个技巧,弹出的页面,虽然去不掉,但是我们可以让它向下移动到屏幕之外
- newWin.document.open('text/html', 'replace');
- var testCode=document.getElementById("txtTestCode").value;
- newWin.document.write(testCode);
- newWin.document.execCommand('saveas','','code.htm');//firefox不兼容的主要原因就是因为ff不支持execCommand('saveas','','filename');
- newWin.close();
- }
- </script>
- </head>
- <body>
- <textarea id="txtTestCode" rows="12" cols="75">
- <!doctype html public "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="Keywords" content="YES!B/S!" />
- <meta name="Description" content="This page is from http://Justinyoung.cnblogs.com" />
- <title>CSS/Javascript demo</title>
- </head>
- <body>
- <p>A testpage from<a href="http://justinyoung.cnblogs.com/" title="博客园Yes!B/S!博客">YES!B/S!</a></p>
- </body>
- </html>
- </textarea>
- <div style="clear:both;">
- <INPUT onclick="runCode()" type=button value=运行代码>
- <INPUT onclick="copyCode()" type=button value=复制代码> <INPUT onclick=saveCode() type=button value=另存代码>
- </div>
- </body>
- </html>
- 在网页上运行代码
- 运行在网页上的linux
- Applet程序无法在网页上运行
- 怎么在Eclipse上运行静态网页
- 可以在网页中点击运行代码
- 在网页上显示html代码
- 使用Docker在Nginx上运行简单的HTML网页
- 网页运行js代码
- 如何在网页中运行自己的代码
- JavaScript学习---在网页中实现运行代码的功能
- 在网页上显示农历公历的JavaScript代码
- 一段在网页上播放mp3语音的代码
- 在网页上Run Code (测试代码) 功能
- 在ipad上网页的一些优化代码
- 在网页上显示所在地天气预报的html代码
- 在HTML网页上打印需要的内容,JS代码
- 在ActionScript Virtual Machine 上运行C/C++代码
- 确保测试代码不会在发布版上运行
- 一个字节造成的巨大性能差异——SQL Server存储结构
- c++ 字符串常用处理函数
- array of const
- [转]如何选择正确的SQL Server Compact安装包
- WINCE注册表应用
- 在网页上运行代码
- [转]进程间通信的11种方法
- Linux, Solaris, 和Mac上的下载工具 : Sun Download Manager
- [转]Xvid参数详解
- C#.NET多线程编程(1):多任务和多线程
- 本本风扇出问题了
- Windows 2000的动态磁盘管理
- vb列表框ListBox的详细使用方法
- MMC System Nodes Are Missing