doPostback and then navigate to another page - javascript

I am trying to call __doPostBack from within JavaScript and then navigate to another page. The script I have is:
$(document).ready(function() {
$("a.change-link").click(function(){
__doPostBack("","");
location.href='pageToGoTo';
});
});
The code is not getting to the location.href='pageToGoTo' line after the post back.
Is there anyway to achieve this functionality?
Thanks

location.href='pageToGoTo'; will never be executed since you're posting the entire form back to the server. You'll need to redirect the request on the server-side.
Response.Redirect = "pageToGoTo";

Related

Javascript redirect to another page

I have a Javascript function that manages the action done pressing an Input Submit button. I want that, before the function calls return, the user could be redirected to a "mypage.php" in which he will do another action, and, if this action completes successfully, also the Javascript function completes successfully.
Thank you
Redirecting to another page in javascript can be achieved by setting window.location;
BUT, when you redirect to another page the code execution of the javascript in the current page ends, a new page is fetched from the server and its javascript starts.
So if you want code on that page to interact with the code on the first page you need a more complex solution. I can think of two options:
The best solution is the do everything in a single-page app. Instead of redirecting to a new page you would fetch data via ajax, render it into a new div, hide the old div. You would have a single source of javascript, no problems.
Another option is to use iframe to show the new page. You can communicate between the page inside the iframe and the page outside it via messages.

Javascript not sending post parameters in Firefox

I have the following js function that sends a post request back to the main index page. This script is working in Chrome but is not sending any parameters in firefox while still posting back to the page. I am verifying this via the network tab after inspecting the page in firefox.
function addDrive(ans){
$.post("index.php", {add_drive: ans });
location.reload();
}
This function is called via a onClick on a button I have placed on my page:
<button onClick="addDrive('y')">Add Drive</button>
I have used similar functions in the past that have worked for both firefox and chrome and I just cant figure out why this does not work in firefox.
My suggestion would be doing something like this :
function addDrive(ans) {
$.post("index.php", {add_drive: ans}).then(function() {
location.reload();
});
}
This would ensure that you reload the page only after successfully finishing the post request. You could also provide a success handler instead if you prefer that instead of using this promises API. You could also do the same in an always handler to ensure it reloads the page even if the request fails but that would be subjective to your requirements.
However, I would argue that this doesn't look like good practice at all, if you have to reload the page you could just have a form and post that instead of trying to post using the jquery handler. You could do something like form.submit() in javascript and that would submit the form and it would submit the data in the form by a post request as long as the method on the form is set to 'POST', that way you don't have to reload the page manually and you can do that from the server end.
Try to reload your page after successful post:
$.post("index.php", {add_drive: ans}).done(function( data ) {
location.reload();
});

HTML5 executing query without postback

I am using html5, javascript and JSP for my project. I want to know if there is some method that i can used to execute a query from my servlet without actually posting back the page. i know it can be done in ASP.net but i do n't how it can be be done in java script and JSP. Actually i have a dynamic webpage displaying data from server.what i want is that in a click event of button i want to execute a query form server and update it on the page. i know i can submit the form but it will submit the page which i want to avoid.Any suggestion......
regards
nquazi
You can use an AJAX request to submit inputs and get back an output without reloading that page. Here is a previous stackoverflow answer that shows you how to do a HTTP GET request.
HTTP GET request in JavaScript?
You will then need to process your inputs, run the query, and send back an output on the backend server.

Rails3 Redirect for javascript/ajax

I'm using a form with the remote => true. I have a validation that is working correctly.
My problem is when the user enters all the correct information and writes the data at this time I do a redirect to the page of the show. Instead of redirecting, it displays the html of the show in my div. How can I make a javascript redirect that will understand? I did not want to use the window.location.
Thanks...
you need to change your Update view into an update.js.erb and supply the javascript you wish to return with. i typically use an ajax flash notice as the response.
here is a good walkthrough:
http://www.whatcodecraves.com/articles/2008/12/13/rails_flash_with_ajax/

Run Javascript Function before response.redirect in classic asp

I have some asp code where I need to execute some javascript function and then redirect the user to a new page. when i do this ibn my code, I am response writing the javascript to the browser. the issue that i am having is that the redirect occurs on ther server side, before the JS function finishes. Is there a work around for this? Thanks
Do the redirect in javascript. At the end of your javascript function:
document.location.href = 'http://url-of-the-next-page';
I'm skeptical that there is a good reason to do it this way, but if you need to solve this problem I suggest you use JavaScript to do the redirection as well.
That's the only way...ASP is going to process the entire script and can't wait for any client side script to finish.

Categories