Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the JavaScript source code in an html page

Tags:

javascript

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.

like image 449
Paul Avatar asked Aug 24 '26 00:08

Paul


2 Answers

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.

like image 145
nickf Avatar answered Aug 26 '26 12:08

nickf


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>
like image 40
statenjason Avatar answered Aug 26 '26 12:08

statenjason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!