1

I want to show javascript code which used in the page by javascript without executing the code. I already used .getScript() method, but it executed the script.

Please tell me how to get javascript code without execution and embed to HTML.

html

<html>
<head>
    <script src='iwanttoshow.js'></script>
</head>
<body>
    <div class="js-source"></div>
</html>

javascript file

$.getScript("/iwanttoshow.js", function(data) {
    window.alert("loaded!");
    $(".js-source").html(data);
}
  • 1
    Sounds like you did not even read the documentation of jqueries `getScript()`? The documentation clearly states: "Load a JavaScript file from the server using a GET HTTP request, then execute it.". _Why do people ask here instead of simply looking into the documentation?_ – arkascha Jan 18 '15 at 06:46
  • 1
    What do you mean "without executing", the script file is added in the head as well, that surely will execute. Do you mean just show the javascript file as preformatted code without running it? If so, do a regular GET request for the file, and output it in `
    ` tags
    – adeneo Jan 18 '15 at 06:50
  • 1
    @arkascha knowing how to look for resources and solutions is also something people needs to learn. – JAR.JAR.beans Jan 18 '15 at 06:51
  • 1
    @JAR.JAR.beans Come on. Typing "jquery getScript" into google is not exactly rocket science... – arkascha Jan 18 '15 at 06:52
  • @arkascha I've read documents and knew getScript execute when it loaded. Maybe, load() is what I want to know. I will try it later. – Kazufumi Takashina Jan 19 '15 at 01:05

3 Answers3

2
<html>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <div>
        <!--Block to embed the js content, use <pre> tag-->
        <pre id="code">
        </pre>
    </div>
    <script>
        //test.js is your own file name
        $("#code").load("test.js");
    </script>
</html>

Jquery load function allows you to load resources.

Use <pre> tag to show the content of javascript.

Bandon
  • 809
  • 1
  • 6
  • 14
1

you can try the jquery .get() method:

Example:

$.get("iwanttoshow.js",function(data,status){
  alert("Data: " + data + "\nStatus: " + status);
},"html");

for more information:

http://api.jquery.com/jquery.get/

http://www.w3schools.com/jquery/ajax_get.asp

ViROscar
  • 148
  • 1
  • 11
0

If you want formatted code - the best solution I've come across is this one:

    <pre>{JSON.stringify(theCodeItself, null, 2)}</pre>

(This example is for the case of using React to build your frontend. But you get the idea.)

pesho hristov
  • 1,946
  • 1
  • 25
  • 43