简体   繁体   中英

Asp.net mvc, onload functions from Layout AND Webpage not working

I'm working on an asp.net mvc project and i have a problem with some javascripts functions.

I would like to execute functionA (from layout) & functionB (from a webpage) when the page is loading.

_Layout.cshtml:

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
    </head>

    <body>
    </body>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script>
        function functionA() {
                alert("Hello from Layout");
        }
        window.onload = functionA;
    </script>
</html>

Webpage.cshtml:

@{
    ViewData["Title"] = "***";
}
<div>
...    
</div>
   
@section Scripts
{
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script>
    function functionB() {
        alert("Hello from WebPage");       
    }
    window.onload = functionB;
    </script>
}

window.onload is only working for the layout so when i'm going the the Webpage i can see "Hello from Layout" and not "Hello from Webpage".

Do you have an idea to perform both functions?

Thanks:).

I managed to make it work like this:

window.onload = function () {
      myFunction();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM