jQuery - Executing another javascript file

89 views Asked by At

In jQuery, how do I execute another javascript file?

I have the following files:

  • folder/page.html
  • folder/objects/control.js
  • folder/objects/test.js

In the page.html, I have the following code:

<script src="objects/control.js" type="text/javascript"></script>

In the control.js, I have the following code:

var url = "objects/test.js";
$.getScript( url, function() {
    alert("test");
});

In the test.js, I have the following code:

alert("test script loaded");

How can I load and execute the test.js javascript file from the control.js file?

I am getting the following console error:

XMLHttpRequest cannot load file:///Path/objects/test.js?_=1434593302488. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

I am running this locally.

2

There are 2 answers

0
Brooklyn Web Developers On BEST ANSWER

Are you running this project locally? If so try running it on WAMP, or MAMP, or some other local web server.

0
Htin Aung On

I think your url is wrong. Try below code.

var url = "/objects/test.js";
$.getScript( url, function() {
    alert("test");
});