How to read javascript source code in html page

Is there a way that a JavaScript function can read the JavaScript source code of other functions on the html page so that the function can do some validation of the job in the javascript source code of these other functions?

Perhaps a walk is how to get the source code for all the JavaScript functions in an HTML page.

+3
source share
2 answers

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

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

I'm not sure how you read the source code outside the function, or even why you especially would like to do it.

+4
source

If you want the entire script tag to 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>
+3
source

Source: https://habr.com/ru/post/1749345/


All Articles