tags:

views:

15364

answers:

4

write some code lie this,

`<form id="abc">
  <input type="text" id="txt" />
</form>

` and now I want to redirect like this,

var temp = $("#txt").val();
url = "http:abc.com/" + temp;
window.location.replace(url);
or
window.location(url);

Is there anyway in jquery to solve this because it's still let me have url = http://abc.com please give me a hand I need that! Thank a lot of viewing my trouble!

+4  A: 

tell you the true, I still don't get what you need, but

window.location(url);

should be

window.location = url;

a search on window.location reference will tell you that.

balexandre
+2  A: 

jQuery does not have an option for this, nor should it have one. This is perfectly valid javascript and there is no reason for jQuery to provide wrapper functions for this.

jQuery is just a library on top of javascript, even if you use jQuery you can still use normal javascript.

Btw window.location is not a function but a property which you should set like this:

window.location = url;
Pim Jager
+8  A: 

As mentioned in the other answers, you don't need jQuery to do this; you can just use the standard properties.

However, it seems you don't seem to know the difference between window.location.replace(url) and window.location = url.

  1. window.location.replace(url) replaces the current location in the address bar by a new one. The page that was calling the function, won't be included in the browser history. Therefore, on the new location, clicking the back button in your browser would make you go back to the page you were viewing before you visited the document containing the redirecting JavaScript.
  2. window.location = url redirects to the new location. On this new page, the back button in your browser would point to the original page containing the redirecting JavaScript.

Of course, both have their use cases, but it seems to me like in this case you should stick with the latter.

P.S.: You probably forgot two slashes after http: on line 2 of your JavaScript:

url = "http://abc.com/" + temp;
Mathias Bynens
A: 

I think that your script does not return false when click on submit.
I write the form here:

<form id="form1" onSubmit="submitFunction(); return false">
    <input type="text" id="txt" />
</form>

With your submit function is still same as before, this should be redirect browser to http://abc.com/xyz with xyz entered in text field.
The problem is when you press enter in the text field #txt, after the script execution is done, the browser continue submit your form once more time. Then the result is the location change to http://abc.com/?name=xyz (Because your input field #txt has empty name, then the string name is used as its name, and the form #form1 has no method value so its method is GET by default).

Bang Dao