最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - How to open URL in same window using javascript? - Stack Overflow

programmeradmin1浏览0评论
<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 0
Add a ment  | 

5 Answers 5

Reset to default 5

It'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

发布评论

评论列表(0)

  1. 暂无评论