Load a javascript function while page loads

1.6k views Asked by At

Hi I want to run a javascript function when the page loads. But as my page derives from the master page there is no form . The is my aspx file

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">
    var m_Names = new Array();

    function LoadArray() {
        PageMethods.Load_Array(onSucceeded, onFailed);
    }
    function onSucceeded(result, userContext, methodName) {
        m_Names = result;
    }

    function onFailed(error, userContext, methodName) {
        alert("An error occurred")
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager> 
<asp:TextBox ID="txt_model_code" runat="server"></asp:TextBox><br />
<br />
<input type="button" value="db Function" /><br />
</asp:Content>

I want to run the LoadArray() function initialy when the page loads. This function is calling a pagemethod given in aspx.vb code file..

Partial Class test3
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()>
Public Shared Function Load_Array() As String()
    Dim Model_Name_old As String()()
    Dim mod_code As String()
    Dim mod_name As String()
    Dim cod_upper As Integer

    //calling webservice that retunrs a jagged array
    Dim ins As New localhost_insert_model.dbModel

    Model_Name_old = ins.get_Model_Name("A")

    mod_code = Model_Name_old(0)
    mod_name = Model_Name_old(1)

    Return mod_name
  End Function
End Class

So how can i load the javascrip LoadArray() function onPageLoad in this scenario??

4

There are 4 answers

0
AudioBubble On BEST ANSWER

This one should work

<script language="javascript">
var m_Names = new Array();

window.onload = function () 
    {
        LoadArray();

    }
 .....your functions    
</script>
2
Nirmal On

I think you can use $document.ready() from jQuery.

0
andersand On

If you don't need the whole page to have loaded completely you could just call it after you created the functions?

var m_Names = new Array();

function loadArray() {
    PageMethods.Load_Array(onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
    m_Names = result;
}

function onFailed(error, userContext, methodName) {
    alert("An error occurred")
}

loadArray();
0
Genady Sergeev On

You can use this code:

Sys.Application.add_load(function(e) { LoadArray(); });

Is its name suggest, this is a page_load handler, which pretty much duplicates in functionality is server-side counter part.