关于 jquery:如何在页面加载前运行 JavaScript 代码? | 珊瑚贝

How to run JavaScript code before page load?


我在 asp.net 网络应用程序中工作。这里我需要在页面加载之前运行 JavaScript。

我试过了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<body oninit=“funinit();” onprerender=“funRender();” onload=“funload();”>

</body>

 <script type=“text/javascript” language=“javascript”>
    function funinit() {
        alert(“funinit”);
    }
    function funload() {
        alert(“funload”);
    }
    function funRender() {
        alert(“funRender”);
    }

这里只有 funload() 工作。

  • stackoverflow.com/questions/5666037/…


您可以将 window.onpaint 用于以下目的:

1
2
3
4
5
6
<script type=“text/javascript”>
    function preloadFunc()
    {
        alert(“PreLoad”);
    }
    window.onpaint = preloadFunc();

希望对你有帮助….

  • 仅供参考:onpaint doesn’t work currently, and it is questionable whether this event is going to work at all,更多信息请访问 stackoverflow.com/a/23657265/216084
  • 根据 MDN 文章,不仅 onpaint 是非标准的并且在 Firefox 上不起作用(因此它不是一个好的跨浏览器解决方案),而且它还在 onload 事件之后运行
  • @user216084 你的评论是在这个答案后 4 年!


只是内联吗?

1
2
<script type=‘text/javascript’>
alert(“funload”);

或者把它放在一个函数中并立即调用它。尝试将它放在页面的最顶部,但是由于 DOM 尚未加载,但您无法获取任何其他元素。

你想做什么?


只需在您希望它运行的主体内的任何位置插入一个标签。只要解析器不引用尚未创建的元素,它就会在解析器读取后立即执行


尝试将您的脚本放在页面的头部:

1
2
3
4
5
6
<head>
  <script type=“text/javascript” language=“javascript”>
        alert(“funinit”);
        alert(“funRender”);
 
</head>

为什么不使用 ClientScriptManager.RegisterClientScriptBlock Method
http://msdn.microsoft.com/en-us/library/btf44dc9.aspx


来源:https://www.codenong.com/6944037/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9285.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?