简体   繁体   English

Javascript函数未打印到控制台上

[英]Javascript function not printing onto the console

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: 在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 未捕获的ReferenceError:未定义大声笑

script tags with an src attribute cannot have inline content as well. 具有src属性的script标签也不能具有内联内容。 You need to create a new script block for that. 您需要为此创建一个新的脚本块。

According to the official html specification: 根据官方的 html规范:

If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI 如果src具有URI值,则用户代理必须忽略元素的内容,并通过URI检索脚本

For details see the Reference 有关详细信息,请参见参考

From the w3c . w3c开始

If the src attribute is not set, user agents must interpret the contents of the element as the script. 如果未设置src属性,则用户代理必须将元素的内容解释为脚本。 If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI. 如果src具有URI值,则用户代理必须忽略元素的内容,并通过URI检索脚本。

You should clear everything between the <script> tags with a src attribute and put your codes in a separate <script> tag. 您应该使用src属性清除<script>标记之间的所有内容,并将代码放在单独的<script>标记中。

<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 . 参见DEMO

Okay, after your edit, scene_world.js is fine. 好的,编辑后, scene_world.js很好。 Why use document.write(..) ? 为什么要使用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 ? 您是否已将这些脚本标签插入headbody内部?

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. 一个粗心的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM