How to run JavaScript code before page load?

前端 未结 5 1755
情深已故
情深已故 2020-12-05 10:15

I have working in asp.net web application. Here I need to run JavaScript before page load.

I have tried:



        
相关标签:
5条回答
  • 2020-12-05 10:16

    Just inline it?

    <script type='text/javascript'>
    alert("funload");
    </script>
    

    Or put it in a function and call it immediately. Try to put it to the topmost of your page, however since the DOM isnt loaded yet you cant get any other elements.

    What is it you want to do?

    0 讨论(0)
  • 2020-12-05 10:23

    Why not Use the ClientScriptManager.RegisterClientScriptBlock Method http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

    0 讨论(0)
  • 2020-12-05 10:24

    just insert a <script> tag wherever inside the body you want it to run. it will be executed as soon as the parser reads it, as long as it doesn't reference an element not yet created

    0 讨论(0)
  • 2020-12-05 10:35

    try to put your script in head section of the page:

    <head>
      <script type="text/javascript" language="javascript">
            alert("funinit");
            alert("funRender");
      </script>
    </head>
    
    0 讨论(0)
  • 2020-12-05 10:42

    You can use window.onpaint for such purpose like :

    <script type="text/javascript">
        function preloadFunc()
        {
            alert("PreLoad");
        }
        window.onpaint = preloadFunc();
    </script>
    

    I hope it helps you....

    0 讨论(0)
提交回复
热议问题