- <p>
- <textarea id="con" style="height: 167px; width: 375px; margin: 2px;">
- <script type="text/javascript"> alert("alert弹出?"); </script>
- </textarea>
- </p>
- <p>
- <input id="btn" onclick="btn();" type="button" value="运行代码" />
- </p>
- <p>
- </p>
- <p>
- <textarea id="theCode" style="width: 450px; height: 300px;" title="双击运行代码"
- ondblclick="runCode('theCode');">
- 这里放入运行的html代码。
- </textarea>
- <br />
- <input onclick="runCode('theCode')" type="button" value="运行代码" />
- <input onclick="doCopy('theCode')" type="button" value="复制代码" />
- <input onclick="doSave('theCode','test')" type="button" value="保存代码" />
- </p>
- <script type="text/javascript">
- // <![CDATA[
- //执行代码
- var btn = document.getElementById("btn");
- var con = document.getElementById("con");
- if (btn && con) {
- btn.onclick = function() {
- var newwin = window.open('', "_blank", '');
- newwin.document.open('text / html', 'replace');
- newwin.opener = null;
- newwin.document.write(con.value);
- newwin.document.close();
- }
- }
- function runCode(obj) {
- var winname = window.open('', "_blank", '');
- var content = document.getElementById(obj).value;
- winname.document.open('text / html', 'replace');
- winname.opener = null
- winname.document.writeln(content);
- winname.document.close();
- }
- //复制代码
- function doCopy(obj) {
- var codeObj = document.getElementById(obj);
- clipboardData.setData('text', codeObj.innerText);
- alert('已经复制代码');
- }
- // 另存为文件
- function doSave(obj, filename) {
- var win = window.open('', '_blank', 'top = 10000');
- var code = document.getElementById(obj).innerText;
- win.opener = null;
- win.document.write(code);
- win.document.execCommand('saveas', true, filename);
- win.close();
- }
- // ]]>
- </script>
来源: