tags:

views:

98

answers:

4

Hi,

      $("#displayPanel #save #saveForm").live("click", function(){

                   //Move to forms/homepage
 });

Wat i m trying is to WHen clicking on the saveForm Button i want to move to a URL(homepage) Where ii had some list of saved forms..

I m new to JQuery ..Please suggest me.. Note:

       Where my saveForm is like
       <input id='saveForm' type='Submit' value='Save Form'>
A: 

You mean redirect to page?

document.location = 'http://yourpage';

Dan Sosedoff
Ya i want to redirect to homepage
Aruna
well, if you want use ajax in proper way you should use json, for example. $('#object').click(function() { $.post('http://yourpage/blabla/', params, function(resp) { if (resp.result == 'ok') { document.location = 'http://some_other_place_to_go'; } else alert('Some error'); }}
Dan Sosedoff
Thanks yaar.....
Aruna
document.location should be avoided. It doesn't work on all browsers
Philippe Leybaert
A: 

you can change the location using javascript by:

document.location = 'http://google.com';

But why are you doing it this way? In this scenario it is usually much simpler to POST the page, and redirect at the server side. You don't really need JavaScript for that.

Kobi
A: 

As the others have said, if you want to change the page, just use document.location. I do have a few other tips for you though:


$('#displayPanel #save #saveForm')

This is unnecessary and slower to run. Just change it to $('#saveForm') and the query will only need to do one lookup.

.live('click', ...

Given how specific your selector is, I'd suggest that using live is also unnecessary. live is meant to be used to apply events to large numbers of elements which may be added to the DOM at any point after the binding. Since you've only got one element with the id saveForm, just apply a click handler to that element normally.

Though, all that said, if all you want is your button to post the form and take the user to the homepage, you don't need any javascript or jQuery, just plain HTML.

<form method="post" action="homepage.html">
    <input type="submit" value="Go to homepage" />
</form>
nickf
Just a small addition: the reason why nickf says your query is unnecessary is that you look for an element with the "saveForm" ID. In HTML we ID's need to be unique, so there can not be more than one "saveForm" element. $("#XXXXX") should always return always one item. Your query looks first for the element displayPanel, then it looks in its children for the element save, and then for saveForm.
Gidon
+2  A: 

You shouldn't use document.location. It has been replaced by window.location.

From the Mozilla docs:

document.location was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, use window.location instead.

The only cross-browser way to navigate to a new URL from javascript is:

window.location = "http://mysite.com/url";
Philippe Leybaert