Reputation: 2278
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
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
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
Reputation: 1038710
In your code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ClientScript.RegisterStartupScript(GetType(), "key", "someFunction();", true);
}
}
Upvotes: 6
Reputation: 10715
<html>
<head>
</head>
<body onload="javascript:yourFunctionCall()">
</body>
</html>
Upvotes: -1
Reputation: 4406
Try out the Page.IsPostback property http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
Upvotes: 1