views:

77

answers:

2

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 checking 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.

+4  A: 

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.

nickf
I edited my question to make it bit clearer. Basically, I'd like to have a function that can checking other functions.
Paul
+3  A: 

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>
statenjason