开发者

How to read the JavaScript source code in an html page

Is there a way that a JavaScript function can read the JavaScript source code of other functions in an html page, so that the function can do some check开发者_运维技巧ing job on the javascript source code of these other functions?

Maybe a walkabout is how to get the source code of all JavaScript functions in an HTML page.


If you want to see the source of a function, you can use the toSource() function:

function x(a) {
    return a + 1;
}
console.log(x.toSource());
// "function x(a) {
//    return a + 1;
// }"

I'm not sure about how you'd read the source outside of a function, or even why you'd particularly want to do this.


If you want the entire script tag try the following:

<html>
<head><title>Testing</title></head>
<body>
This is body text.

<input type="button" onClick="javascript:readScript()" value="Read it!" />

<script type="text/javascript" id="myscript">
    function readScript(){
        var html = document.getElementById("myscript").innerHTML;
        alert(html);
    }
</script>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜