how to redirect to a new page using javascript after executing the function inside button click event
I have a page which is an aspx page. This is rendering inside an iframe inside the main asp site. Now I have a button say Cancel.
I have to do some cancel processing and then redirect to some other asp page. I cannot use response.redirect with the page name as what its doing is its reloading the redirected site inside my frame so i am getting two headers etc.
I figured out a javascript "javascript:parent.change_parent_url('/home.asp');" when i put the as link eg
<a href="javascript:parent.change_parent_url('/开发者_StackOverflow社区nestedpage/statuspage.asp');"><u> here</u> </a>
But my main problem is in the example above it was just a link with nothing else to do other than redirect to main page. So it worked.
But with the button I want to finish some processing before calling the redirect through javascript.
I am confused as to how the flow of events should be.
Would appreciate any inputs or ideas.
Thanks.
To redirect a page in javascript use window.location="http://someplace.com";
if you would like to perform some processing prior to the redirection you will do something like this
function DoSomeProcessing() {
//Do work...
}
function ProcessAndRedirect(URL) {
DoSomeProcessing();
window.location = URL;
}
//The link should look like this
<a href="javascript:void(0);" onclick="ProcessAndRedirect('http://someplace.com')">Click me</a>
To redirect a specific iframe your code would look like this.
window.frames["MyIframe"].src = "http://someplace.com";
Also if its reloading the entire site inside the iframe it sounds to me like you might be redirecting to the incorrect page. Verify which page is actually contained in the iframe and make sure your iframe redirect is pointing to it.
This can be much more elegant using JQuery.
<a id="myLink">Click me</a>
<script type="text/javascript">
$("a#myLink").click(
function () {
window.location.replace("http://someplace.com");
return false;
}
);
</script>
I hope it helps.
精彩评论