Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在执行html时调用javascript函数_Javascript_Html - Fatal编程技术网

在执行html时调用javascript函数

在执行html时调用javascript函数,javascript,html,Javascript,Html,我有一个任务,当我打开html页面时,html在script标记下包含javascript,我在其中实现了一个名为auther()的函数。如何在打开html页面时调用此函数,我不想使用html/javascript中的onClick或onLoad等方法来实现它 意味着每当我的html被执行时,我的javascript函数应该被自己调用。以下是我的html: <html> <head> <title>Demo</title>

我有一个任务,当我打开html页面时,html在script标记下包含javascript,我在其中实现了一个名为
auther()
的函数。如何在打开html页面时调用此函数,我不想使用html/javascript中的
onClick
onLoad
等方法来实现它

意味着每当我的html被执行时,我的javascript函数应该被自己调用。以下是我的html:

<html>
   <head>
      <title>Demo</title>
   </head>
<body>
  <div id="authOps" >
      <button onclick="auther();">GET author</button>
      //By clicking on this button it is working but I want to execute 
      //this function when this html get executed
  </div>
</body>
<script type="text/javascript">
   function auther(){
      //some code
   }
</script>
</html>

演示
获取作者
//通过点击这个按钮,它正在工作,但我想执行
//此函数在执行此html时使用
函数auther(){
//一些代码
}
在上面的场景中,每当打开我的html页面时,javascript函数都应该在没有任何单击处理的情况下执行


注意:我提到的html代码仅供参考,我的主要目的只是执行javascript函数,我如何才能实现这一点???

只需在脚本块中调用该方法,而不是通过单击按钮

<script>auther(); </script>
auther();

据我所知,有两种方法可以做到这一点

1:


演示
获取作者
//通过点击这个按钮,它正在工作,但我想执行
//此函数在执行此html时使用
函数auther(){
控制台日志(“Hi”);
};
auther();

我也尝试过这个方法,但在运行时浏览器抛出异常,说undefined methodexact错误是“Uncaught ReferenceError:auther未定义”,这是因为您在声明它之前调用了它。这就是为什么它在@Gowtham shiva中工作的原因。作为第一个示例
,javascript函数应该在没有任何点击处理的情况下执行,为什么处理程序在没有触发事件的情况下运行?而且servlet只是输出html,它本身不做任何html处理