1

Why the following is not being printed to the console? I cant understand what the error is ..can Some one help me debug the error.

<script type="text/javascript" src="static/js/scenes/scene_world.js">
  //document.write("<button type='button' onclick='click();'>Click Me!</button>");
  //document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
    console.log("lol");
  </script>

inside scene_world.js:

  function lol(){
  console.log("lol");
  }

Tired accessing it from outside like this:

  <script type="text/javascript" src="static/js/scenes/scene_world.js">
  //document.write("<button type='button' onclick='click();'>Click Me!</button>");
  //document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");

  </script>
  <script>
  document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
  </script>

But it is giving me this error:

Uncaught ReferenceError: lol is not defined

user2409375
  • 115
  • 1
  • 12

6 Answers6

4

script tags with an src attribute cannot have inline content as well. You need to create a new script block for that.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
2

According to the official html specification:

If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI

For details see the Reference

jantimon
  • 36,840
  • 23
  • 122
  • 185
2

From the w3c.

If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

You should clear everything between the <script> tags with a src attribute and put your codes in a separate <script> tag.

<script type="text/javascript" src="static/js/scenes/scene_world.js"></script>
<script>
document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
</script>

See DEMO.

Antony
  • 14,900
  • 10
  • 46
  • 74
0

Okay, after your edit, scene_world.js is fine. Why use document.write(..)?

<script type="text/javascript" src="static/js/scenes/scene_world.js">
</script>
<input id='clickMe' type='button' value='clickme' onclick='lol();'/>

Should work when button is clicked.

For direct call:

<script type="text/javascript" src="static/js/scenes/scene_world.js">
</script> 
<script type="text/javascript">
    lol();
</script>

Have you inserted those script tags inside head or body?

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • i just wanted to make it work in javascript. But with or without the script its not calling the function in scene_world.js. And document.write was being used in the main html file – user2409375 Jun 13 '13 at 10:35
  • i want it to based on click of a button. See still it is not calling the function in scene_world.js Is it because it cant recognize the function in this script tag? – user2409375 Jun 13 '13 at 10:38
  • even the direct call is giving me an error. Everything i wrote is proper isnt it? – user2409375 Jun 13 '13 at 10:43
  • Is the path to `scene_world.js` correct? Press `CTRL+U` in your browser, find this script tag and click on it's `src` attribute. It should show the source of `scene_world.js`. – ElmoVanKielmo Jun 13 '13 at 10:43
  • Path and all are right. What i cannot figure out is why it is not invoking the function – user2409375 Jun 13 '13 at 10:47
0

It was not printing on to the console because the function i tried invoking was inside another function. So it was not properly invoked. A careless mistake.

user2409375
  • 115
  • 1
  • 12