0

I have a javascript file named "a.js" located in host a. The code in a.js is as below:

function sayHello() {
    alert("Hello, world");
}

I have a html file named b.html located in host b, in which there are code like below:

<html>
  <head>
    <script type="text/javascript" src="http://a/js/a.js" />
    <script type="text/javascript">
      sayHello();
    </script>
  </head>
  <body>
  </body>
</html>

When I key in the url "http://b/b.html" in my browsers (I have tested in IE8, Chrome), my browsers don't pop the alert window. Did I miss something configuration ?

How should I do so that my b.html can include a javascript file which is located in another host and the functions in the javascript file can be executed just like we include Google's javascript API files ?

Thank you for your help.

Nix
  • 57,072
  • 29
  • 149
  • 198
wureka
  • 731
  • 1
  • 11
  • 26

2 Answers2

3

There are no security restrictions on including javascript from a different domain (unlike ajax calls which have to reside on one domain) so you must be mistyping something or the file isn't available. Make sure you can view the js by itself in the browser first by just going to the url directly, then copy and paste it into your src tag. It will work (if it's working javascript!) :)

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
3

You're having this problem because the <script> tag is not self-closing (unless you're serving your file with a XML content-type). As Dr.Dredel said, in this case it's not related to any kind of security restriction. Change your markup to:

<script type="text/javascript" src="http://a/js/a.js"></script>

Further reference: Why don't self-closing script tags work?

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150