window.location.href not working
Asked Answered
L

4

19

My website is http://www.collegeanswerz.com/. I'm using rails. The code is for searching for colleges. I want the user to be able to type in the colleges name, click enter, and be taken to the url, rather than seeing the search results (if the user types in the name properly. if not I want to show the search results). I'm using "a" and "stackoverflow.com" as placeholders while I try to get it to work.

I'm using window.location.href based on this: How to redirect to another webpage in JavaScript/jQuery?

javascript

$("#search_text").submit(function() {
    if ($("#search_field").val() == "a")
    {
        window.location.href = "http://stackoverflow.com";
        alert('worked');
    }
});

layout file

<%= form_tag("/search", :method => 'get', :id => 'search_text', :class => 'form_search') do -%> 
    <div id="search"> <%= search_field_tag :search, params[:search], :placeholder => 'enter college', :id => "search_field", :class => 'input-medium search-query' %></div> 
<% end -%>

static_pages_controller.rb

def search
  @colleges = College.search(params[:search])
end

The alert is working, which tells me that the things inside the if statement should be being executed. But it's taking me to the normal search results instead of stackoverflow.com. Why is this?

Lermontov answered 18/8, 2013 at 15:24 Comment(6)
I see that I got downvotes. Could you let me know why? I don't see anything wrong with this question on first thought.Lermontov
I didn't downvote the question, but at the very least, you should show the HTML of the form.Calfskin
Sure, I'll edit the question.Lermontov
possible duplicate of window.location() not working, not opening pageEmmanuelemmeline
Show the generated HTML, not the Ruby code.Emmanuelemmeline
Yeah, you're right @JuhanaLermontov
S
48

The browser is still submitting the form after your code runs.

Add return false; to the handler to prevent that.

Seeder answered 18/8, 2013 at 15:30 Comment(3)
Thank you, you're right, it worked. This is addressed here too (#6094630). I didn't realize that before I asked the question.Lermontov
You just saved my life.Lanthorn
But if i return false, the submit function is of no use.Coniferous
W
2

Try this

`var url = "http://stackoverflow.com";    
$(location).attr('href',url);`

Or you can do something like this

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

and add a return false at the end of your function call

Whitebeam answered 18/8, 2013 at 15:31 Comment(6)
May I know the reason why I got a down Vote ? @Adam ZernerWhitebeam
You added the correct answer (return false) long after you got those downvotes.Emmanuelemmeline
@Juhana Was editing my Answer and in the mean time got those down votes. But doesn't it make sense to add comment prior down-voting as these down voting will make me think to answer any of the question nowWhitebeam
If it makes you think before you answer... mission accomplished.Emmanuelemmeline
@ArihantGodha: Giving a wrong or bad answer will get the answer downvoted. Editing it later so it says what another answer already says (intentionally, or not) will not reverse that. When you edit your answer such that it becomes just a duplicate of another, delete it.Calfskin
var url = "http://stackoverflow.com"; $(location).attr('href',url); this worked for me. Thanks dude.Wifeless
W
1

Please check you are using // not \\ by-mistake , like below

Wrong:"http:\\stackoverflow.com"

Right:"http://stackoverflow.com"
Wolford answered 21/8, 2017 at 14:33 Comment(0)
U
1

if anyone faced problem even after using return false; . then use the below.

setTimeout(function(){document.location.href = "index.php"},500);
Unexceptionable answered 9/5, 2020 at 1:19 Comment(2)
Your answer not working to me .Decibel
it's working for me. the problem from your side @GanesanJUnexceptionable

© 2022 - 2024 — McMap. All rights reserved.