0

Using JavaScript, I want to retrieve the content of the script files. These script files remain in local(in web page).

For example. In web page, there is a script,

<script src="tool.js"></script>

Latter, I want to get the content of tool.js and process the retrieved result (like dump its content).

I have tried to use jQuery.getScript. However, it tells me that Origin null is not allowed by Access-Control-Allow-Origin.

Mathijs Flietstra
  • 12,563
  • 3
  • 35
  • 66
Yulong Tian
  • 107
  • 1
  • 6
  • Do you run any web server on local? – WooCaSh May 29 '13 at 02:53
  • No, I just need to read script file from website developed by other developers. In other words, I can't control these scripts directly, but I need to process them (like dump their contents in console). – Yulong Tian May 29 '13 at 03:08

3 Answers3

0

try an ajax like the following

$.ajax('/tool.js', {
      type: 'GET',
      crossDomain: true,
      dataType: 'jsonp',
      success: function(data) {
        console.log(data);
      },
      error: function() {
        console.log("call failed");
      }
});
Lars
  • 616
  • 7
  • 8
0

try using jquery.load() and put its contents on an element and use .html()

<html>
</head>
    <script src="jquery.js"></script>
    <script>
    $(document).ready(function(){
        //setup ajax to work locally
        $.ajaxSetup({
           crossDomain: false,
            isLocal :true
        });
        $("#a").load('jquery.js', function() {
          alert($("#a").html());
        });
    });
    </script>
</head>
<body>
    <span id="a" style="display:none;"></span>
</body>
</html>

Browser Check: the code above works and tested in FF, Safari, Opera and IE

but if you keep on having problem with Origin null is not allowed by Access-Control-Allow-Origin then having a web server installed is needed as said here; Origin null is not allowed by Access-Control-Allow-Origin

Reference:

http://api.jquery.com/load/

code for your disposal : http://jsfiddle.net/PeaH3/1/

Community
  • 1
  • 1
Netorica
  • 16,405
  • 16
  • 70
  • 105
0

You need to configure your Access-Control-Allow-Origin:

Access-Control-Allow-Origin: *

This lets you use any resource to obtain assets from an external domain. Do this first and either Mahan's or Lars's solution will work.

More info: Access-Control-Allow-Origin Multiple Origin Domains?

Community
  • 1
  • 1
Francis Kim
  • 4,110
  • 4
  • 33
  • 51