0

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

                   //Move to forms/homepage
    });

What I'm trying is 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. Note:

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

4 Answers 4

3

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";
Sign up to request clarification or add additional context in comments.

Comments

0

You mean redirect to page?

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

3 Comments

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

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.

Comments

0

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>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.