ASP.NET content page: how to run a script on "body" using "body onload = myfunc ()"

I added to the main page my script "myscript.js". Then on the content page, I would like to load myscript () at startup (body onload).

How can i do this?

+3
source share
4 answers

Use

Page.RegisterStartupScript

  if (!IsStartupScriptRegistered (key))
  {
      String script = "<script type=\"text/javascript\">" +
        "alert('Hello World');</" + "script>";
      RegisterStartupScript(key, script);
  }

Or you can use the jQuery library and add a function call to the document.ready function

http://jquery.com/

 $(document).ready(function(){
   // Add your function call here

 });
+3
source

I use ContentPlaceHolderthe home page for this purpose. This allows you to include specific scenarios <head>only when you want it.

+1
source

onload :

:

body id="PageBody" runat="server"

in the content page:

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody");

body.Attributes.Add("onload", "doSomething();");`
+1

onLoad.

0

Source: https://habr.com/ru/post/1749028/


All Articles