0

I have a little trouble: on a server I published a .js file where I declared a function that simplifies the document.write() function:

function print(id, text){document.getElementById(id).innerHTML = text;}

I already tested this code calling the function print("paragraph", "Hello, world!"); and it works, but I don't know why when I created a new .html file and imported the easyjs_demo.js file and in the <script> tag I called the print function, it didn't work:

<body>
      <p id="paragraph"></p>
      <script src="easyjs_demo.js">
      print("paragraph", "Hello, world!");
      </script>
</body>

Do you know how can I import the .js file wothout having bugs like this? Thank You very much!

1 Answers1

5

You can't combine includes and code, try separating Your external file, and the inline code like this:

<script src="easyjs_demo.js"></script>
<script>
  print("paragraph", "Hello, world!");
</script>

Demo Plunker here.


Multiple sources show that if You specify the src attribute, the user agent ignores the inline code:
Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42