<form id="search" action="">
<input id="findroom" type="text" class="defaultText" title="Room Name">
<input id="find" type="submit" value="Find">
</form>
<script>
$(document).ready(function(){
$('#search').submit(function() {
var r = "/rooms/"+$('#findroom').val();
open(r);
});
});
</script>
Having trouble with the code above. I am trying to generate a restful URL for use in a Ruby on Rails app. I can generate the URL I need from the form as the var 'r'. When I use open() as above the link opens fine, but in a new window. I need it to open in the same window. Have tried:
open.(r,'_self');
doesn't work
haven't had any luck with:
location.replace
location.href =
Anybody have any ideas please?
Thanks
Barry
<form id="search" action="">
<input id="findroom" type="text" class="defaultText" title="Room Name">
<input id="find" type="submit" value="Find">
</form>
<script>
$(document).ready(function(){
$('#search').submit(function() {
var r = "/rooms/"+$('#findroom').val();
open(r);
});
});
</script>
Having trouble with the code above. I am trying to generate a restful URL for use in a Ruby on Rails app. I can generate the URL I need from the form as the var 'r'. When I use open() as above the link opens fine, but in a new window. I need it to open in the same window. Have tried:
open.(r,'_self');
doesn't work
haven't had any luck with:
location.replace
location.href =
Anybody have any ideas please?
Thanks
Barry
Share Improve this question asked Dec 12, 2011 at 16:45 BarryBarry 3411 gold badge4 silver badges8 bronze badges 05 Answers
Reset to default 5It's as simple as
window.location = r;
To open a new popup, use
window.open(r);
If you want to redirect the current page, use
window.location = r;
Correct syntax is
window.open(URL,name,specs,replace)
You just need to give _self in name
Please read more
window.open creates a new secondary browser window and loads the referenced resource, whereas windo.location allows for changing the URL of the current window. So what you want is:
window.location = r;
Thank you everyone for your help. You're all on the money, but the answer is
e.preventDefault();
It's to do with the form submitting and overriding the window.location mand. Once you block the default behaviour it works. This is the full code:
<section id="roomsearch">
<form id="search" action="">
<input id="findroom" type="text" class="defaultText" title="Room Name">
<input id="find" type="submit" value="Find">
</form>
<script>
$(document).ready(function(){
$('#search').submit(function(e) {
var r = "/rooms/"+$('#findroom').val();
window.location = r;
e.preventDefault();
});
});
</script>
</section>
Thanks for the time and help.
Barry