实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Dynamic Script Example 2</title>
5
</head>
6
<body>
7
    <p>You should see an alert saying &quot;hi&quot; after clicking the button.</p>
8
    <input type="button" value="Add Script" onclick="addScript()">
9
10
    <script type="text/javascript">
11
    
12
        function loadScriptString(code){
13
            var script = document.createElement("script");
14
            script.type = "text/javascript";
15
            try {
16
                script.appendChild(document.createTextNode(code));
17
            } catch (ex){
18
                script.text = code;
19
            }
20
            document.body.appendChild(script);
21
        }
22
    
23
        function addScript(){
24
            loadScriptString("function sayHi(){alert('hi');}");
25
            sayHi();
26
        }
27
28
    </script>
29
</body>
30
</html>