3

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.

0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.