在body标签之前调用Javascript函数 [英] Calling Javascript function before body tag

查看:75
本文介绍了在body标签之前调用Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为cookieRedirect.js的外部文件中具有Load()函数,我想在标记之前加载该函数.我以前是这样加载的:

I have Load() function in external file called cookieRedirect.js and I want to load that function before tag. I used to load it like this:

<script type="text/javascript" src="cookieRedirect.js"></script>
</head>
<body onload=Load();>

但是,当我尝试此操作时,它不起作用:

But when I try this it's not working:

<script type="text/javascript" src="cookieRedirect.js">
window.onload=Load();
</script>
</head>
<body>

推荐答案

如果要在页面加载之前加载该函数,只需将脚本代码添加到页面的< head> 中,而不是在身体. window.onload onload =" 仅在页面加载时执行代码,而不是之前.

If you want to load that function before page load, just add script code to <head> of the page, not in the body. window.onload or onload="" will execute your code only when page was loaded, not before.

这样做:

<html>
  <head>
    <script src="cookieRedirect.js"></script>
    <script>
      Load();
    </script>
  <head>
  <body>
  </body>
</html>

这篇关于在body标签之前调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆