How to call javascript function on page load in asp.net

解答1,使用RegisterStartupScript来运行

需要注意的是,下面的demo,显示的是执行某一个函数

Calling JavaScript function on code behind i.e. On Page_Load

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

If you have UpdatePanel there then try like this

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

View Blog Article : How to Call javascript function from code behind in asp.net c#

解答2

使用RegisterClientScriptBlock注册onload

或者注册jQuery的ready

JavaScript that executes after page load

解答1

These solutions will work:

<body onload="script();">

or

document.onload = function ...

or even

window.onload = function ...

Note that the last option is a better way to go since it is unobstrusive and is considered more standard.

解答2

$(window).on("load", function(){ ... });

.ready() works best for me.

$(document).ready(function(){ ... });

.load() will work, but it won't wait till the page is loaded.

jQuery(window).load(function () { ... });

Doesn't work for me, breaks the next-to inline script. I am also using jQuery 3.2.1 along with some other jQuery forks.

To hide my websites loading overlay, I use the following:

<script>
$(window).on("load", function(){
$('.loading-page').delay(3000).fadeOut(250);
});
</script>

ScriptManager.RegisterStartupScript

RegisterStartupScript(Control, Type, String, String, Boolean)

Registers a startup script block for a control that is inside an UpdatePanel by using the ScriptManager control, and adds the script block to the page.

Remarks

You use the RegisterStartupScript method to register a startup script block for a page that is compatible with partial-page rendering and that has no Microsoft Ajax Library dependencies. Startup script blocks that are registered by using this method are sent to the page only when the control that is registering the block is inside an UpdatePanel control that is being updated. To register a startup script block every time that an asynchronous postback occurs, use the RegisterStartupScript(Page, Type, String, String, Boolean) overload of this method.

If you want to register a startup script that does not pertain to partial-page updates, and if you want to register the script only one time during initial page rendering, use the RegisterStartupScript method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

The script block that is rendered by the RegisterStartupScript method executes when the page finishes loading but before the page's client onload event is raised. Startup script blocks are located at the bottom of the rendered ASP.NET page just before the </form> tag.

Startup script blocks that are registered by using RegisterStartupScript are not guaranteed to be output in the same order in which they are registered. If the order of the startup script blocks is important, use a StringBuilder object to gather the script blocks in a single string, and then register them all as a single startup script.

RegisterStartupScript(Page, Type, String, String, Boolean)

Registers a startup script block for every asynchronous postback with the ScriptManager control and adds the script block to the page.

Remarks

You use this method to register a startup script block that is included every time that an asynchronous postback occurs. To register a script block for a control that is inside an UpdatePanel control so that the script block is registered only when the UpdatePanel control is updated, use the RegisterStartupScript(Control, Type, String, String, Boolean) overload of this method.

If you want to register a startup script that does not pertain to partial-page updates, and if you want to register the script only one time during initial page rendering, use the RegisterStartupScript method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

How to call javascript function on page load in asp.net的更多相关文章

  1. page load时执行JavaScript

    // Multiple onload function created by: Simon Willison // http://simonwillison.net/2004/May/26/addLo ...

  2. [Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust

    In some cases it’s useful to be able to invoke a JavaScript function inside Rust. This session showc ...

  3. href="javascript:function()" 和onclick的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  4. No Javascript on this page

    在调试SignalR程序时,不知怎样的情况,出现异常: HTTP Error 404.0 - Not Found The resource you are looking for has been r ...

  5. 关于<a href='javascript:function()'>

    <a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...

  6. javascript function对象

    <html> <body> <script type="text/javascript"> Function.prototype.get_my_ ...

  7. Understanding JavaScript Function Invocation and "this"

    Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...

  8. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  9. JavaScript function函数种类介绍

    JavaScript function函数种类介绍 本篇主要介绍普通函数.匿名函数.闭包函数 1.普通函数介绍 1.1 示例 ? 1 2 3 function ShowName(name) {     ...

随机推荐

  1. 基于webstorm卡顿问题的2种解决方法

    基于webstorm卡顿问题的2种解决方法:https://www.jb51.net/article/128441.htm

  2. java基础笔记(8)

    java的多线程 进程:程序的执行过程,持有内存资源 线程:是系统的最小执行单元,共享进程的资源 线程之间可以互斥.也可以同步: Thraed类: 通过一个案例来了解线程Thread类和Runnabl ...

  3. POJ 3549 GSM phone(圆+扫描线+最短路)

    题目意思是求起点s到终点s的最短路,但是只能在圆的内部和边上走.一种可以想到的方法就是求出所有的交点,然后两两连边并验证合法性,但是这样的交点数规模有n2. 我们可以观察发现,我们在圆求并构成的图形中 ...

  4. POJ 3259 Wormholes SPFA算法题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. 禁止input输入框历史记录

    <input type="text" autocomplete="off" />

  6. Kali Linux安装及中文指南

    Kali Linux安装及中文指南 Kali Linux安装教程:https://blog.csdn.net/u012318074/article/details/71601382 Kali Linu ...

  7. Apache 的 httpd.conf 配置文件

    http.conf 是 Apache 的配置文件,Apache 的常见配置主要是通过修该文件实现的,修改之后需要 重启 Apache 服务生效. Httpd.conf #Apache 安装目录 Ser ...

  8. makemap - 为sendmail创建数据库映像表

    SYNOPSIS(总览) [-N ] [-d ] [-f ] [-o ] [-r ] [-s ] [-v ] maptype mapname DESCRIPTION(描述) 创建 sendmail(8 ...

  9. PAT Basic 1036 跟奥巴马一起编程 (15 分)

    美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个 ...

  10. Linux下用命令查看CPU ID以及厂家等信息

    Linux下用命令查看CPU ID // 获得CPU IDdmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}' // 获得磁盘IDfdi ...