Setting the Body OnLoad attribute on the MVC Asp.net homepage

I have a view using the main page containing some javascript that should be executed using the OnLoad body. What is the best way to install OnLoad on my MasterPage for specific views only?

In theory, I tried to pass the name of the javascript function as ViewData. But I do not want my controllers to have to know about javascript on the page. I really don't like this approach ...

<body onload="<%=ViewData["Body_OnLoad"]%>">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />

Edit - I suppose one idea would be to use an event instead to create a jQuery document ...

Any other ideas?

+3
source share
4 answers

MVC , , .js ...

script, (, jquery.js, global.js, jquery-plugins,.css ..)., , (onLoad, onSave ..). .js , , script .aspx. .js, - , .js . , .js, . .js content.js, . , , .js , ( .js , ).

, :

MasterPage.Master Loads: jquery.js, global.js, plugins.js

ContentPage: ContentPage.js

Global.js , -, .

ContentPage.js , - .

+3

, JQuery ASP.NET MVC, . .

JQuery Mustafa, Intellisense:

jquery-1.2.6-intellisense.js

Scripts ( MVC Beta 1) :

<script language="javascript" type="text/javascript" src="../../Scripts/jquery-1.2.6-intellisense.js"></script> 

​​:

$(document).ready(function() {
   // do stuff when DOM is ready
});

, , , :

<asp:contentplaceholder id="JavascriptPlaceholder" runat="server"></asp:contentplaceholder>  

.

+5

: body onload ASP.net 2.0 MasterPages

MasterPage.master

<head>
<asp:ContentPlaceHolder runat="server" id="Headers">
</asp:ContentPlaceHolder>
<script language=javascript>
   function mp_onload() {
     if(window.body_onload != null)
     window.body_onload();
   }
</script>
</head>
<body onload="mp_onload();">

Default.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="Headers" Runat="Server">
<script language="javascript">
   function body_onload()
   {
       //do something
   }
</script>
</asp:Content>
 
+5

jQuery JavaScript.

- , , JavaScript. JavaScript.

0

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


All Articles