博客園 中 運行js

javascript

 


html

 

<p><textarea id="con" style="height: 167px; width: 375px; margin: 2px;">&lt;script type="text/javascript"&gt;
alert("alert彈出?");
&lt;/script&gt;</textarea></p>
<p><input id="btn" onclick="btn();" type="button" value="運行代碼" /></p>
<p>&nbsp;</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>
相關文章
相關標籤/搜索