Mehmet
Mehmet

Reputation: 2278

calling Javascript at page load

I have a Javascript. I want to call only at page load. I dont want to call at postbacks.. (Asp.net 3,5)

Upvotes: 1

Views: 12973

Answers (5)

MattPII
MattPII

Reputation: 256

You can also attach to the load event to avoid accidentally overriding the onload in the body tag with document.onload in JavaScript. This is usually only a problem with master pages having content pages implement their own load event and overriding the master page JavaScript.

<script type="text/javascript">
    function Page_loaded() {
        //Do Work
    }

    if (document.all) {
        //IE
        window.attachEvent('onload', Page_loaded);
    } else {
        //Everything else
        window.addEventListener('load', Page_loaded, false);
    }
</script>

Upvotes: 0

anishMarokey
anishMarokey

Reputation: 11397

Better thing is to call your java script function at the tag on load. Here the java script calls only once.not in all post backs.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

In your code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ClientScript.RegisterStartupScript(GetType(), "key", "someFunction();", true);
    }
}

Upvotes: 6

Barrie Reader
Barrie Reader

Reputation: 10715

<html>
<head>

</head>
<body onload="javascript:yourFunctionCall()">

</body>
</html>

Upvotes: -1

Jan_V
Jan_V

Reputation: 4406

Try out the Page.IsPostback property http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

Upvotes: 1

Related Questions