Every single tiime I go to do a postback I would like to run a javascript subroutine. what is the easiest way to do that without haveing to create an onclick or onchange event for each control
RSS
Before a page does a postback, I want to run a script each time without having to set it to an onclick or onchange event for a control. What is the easiest way to accomplish that?
If you want to call a JavaScript function prior to performing a post-back, you may want to try using the following, which will execute prior to leaving the page (which would occur when performing a postback).
window.onbeforeunload = function(){
//Perform your Javascript Function here
YourFunction();
};
Another method that might work would be to handle this server-side within the Page_Load method of your page :
string scriptKey = "OnSubmitScript";
//Your function that must be available within the page
string javaScript = "YourFunction();";
//Calls the function to execute upon submitting the page
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);
What browser are you using, as I believe there are some issues with onbeforeunload when working certain browsers? Also, have you tried using just the alert with the return statement or removing just the alert to see if results vary?
You could also try adding your event within the body tag of the page :
I'd recommend using the server-side method, or at least giving it a try. I think it might work better for your situation.
string scriptKey = "OnSubmitScript";
//Your function that must be available within the page
string javaScript = "YourFunction();";
//Calls the function to execute upon submitting the page
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);
JAYHAWKER
Participant
1252 Points
1896 Posts
Every single tiime I go to do a postback I would like to run a javascript subroutine. what is the...
Jan 10, 2013 03:53 PM|LINK
Before a page does a postback, I want to run a script each time without having to set it to an onclick or onchange event for a control. What is the easiest way to accomplish that?
Rion William...
All-Star
27906 Points
4618 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 03:58 PM|LINK
If you want to call a JavaScript function prior to performing a post-back, you may want to try using the following, which will execute prior to leaving the page (which would occur when performing a postback).
window.onbeforeunload = function(){ //Perform your Javascript Function here YourFunction(); };Another method that might work would be to handle this server-side within the Page_Load method of your page :
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 04:11 PM|LINK
thanks for the help. I like the wind.oonbeforehand best. I tried it, but it ins't being called. This is what I have.
window.onbeforeunload = function () { //runs before the postback alert('saveDivState'); saveDivState(); };Rion William...
All-Star
27906 Points
4618 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 04:27 PM|LINK
IIRC, window.onbeforeunload requires a return value, you may want to try something like this:
window.onbeforeunload = function () { //runs before the postback alert('saveDivState'); saveDivState(); return true; };JAYHAWKER
Participant
1252 Points
1896 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 04:35 PM|LINK
Still nothing. This is exactly what I have.
<script type="text/javascript" language="javascript"> window.onbeforeunload = function () { //runs before the postback alert('saveDivState'); saveDivState(); return true; };</script>
Rion William...
All-Star
27906 Points
4618 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 04:45 PM|LINK
What browser are you using, as I believe there are some issues with onbeforeunload when working certain browsers? Also, have you tried using just the alert with the return statement or removing just the alert to see if results vary?
You could also try adding your event within the body tag of the page :
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 05:42 PM|LINK
IE 10, also with the return true, I get a message everytime asking if I want to leave the page so I will probably have to find something different.
Rion William...
All-Star
27906 Points
4618 Posts
Re: Every single tiime I go to do a postback I would like to run a javascript subroutine. what is...
Jan 10, 2013 06:41 PM|LINK
I'd recommend using the server-side method, or at least giving it a try. I think it might work better for your situation.