Tuesday, September 13, 2011

calling Javascript function in asp.net 4.0 master page in LookServer

when you have a asp.net applicaiton with master page enable,

you probably will try to call it in the body onload event from the master page.

here is the simple test javasript function embedded at the header section in the html code

<script language="javascript" type="text/javascript">
    function load()
    {
     alert("Page is loaded");
    }
</script>

you should encounter the following error 

 <body id="PageBody" runat="server" onload="load();">

Error:CS1061: 'ASP.views_shared_lookserver_master' does not contain a definition for 'load' and no extension method 'load' accepting a first argument of type 'ASP.views_shared_lookserver_master' could be found (are you missing a using directive or an assembly reference?)


<body id="PageBody" runat="server" onload="Javascript:load();">

Error:Compiler Error Message: CS1026: ) expected


i believe that the look server master page did not like the javascript call with the body onload event.

i google it and i got some good idea from this page

instead of executing javascript functio from the body onload event. the function will be called from the window.onload event.

here is the change of javascript

before :
<script language="javascript" type="text/javascript">
    function load()
    {
     alert("Page is loaded");
    }
</script>

<body id="PageBody" runat="server" onload="load();">

after:

<script type="text/javascript">
 window.onload  = MyInit();

   function MyInit() {
         alert("Page is loaded");
   }
 </script>

<body id="PageBody" runat="server">


 you will see the pop window when the page is loading. cheers..i hope this will help you when you need

to execute a javascript function during the page loading


Update:

when i test the script i got an Error Message (not implement in IE)

here is the fix
Before:
<script type="text/javascript">
 window.onload  = MyInit();

   function MyInit() {
         alert("Page is loaded");
   }
 </script>


Current:
<script type="text/javascript">
 window.onload  =function () {
         alert("Page is loaded");
   }
 </script>

No comments:

Post a Comment