window.location.href does not work, it always returns an alert

window location href not working with https
window.location.href not working on button click
window.location.href error handling
window.location.href not working on form submit
window.location.href not working in ajax success
window.location.href not working in chrome
window.location.href not working angular
window.location.replace not working

I have been learning HTML/CSS/JS in my free time. I'm still a noobie and I have encountered this problem while practicing. I want to create a form where you can type what are you searching for and sumbit button redriects you to google and If it's empty it shows an alert. But the redirect does not work, I always get an alert. Can you guide me what Am I doing work ? Sorry for my english, It isn't my native langue.

<form id="myform">
        <input type="search" name="searchedValue">
        <input type="submit" value="Szukaj">

    $("document").ready(function() {
        $("#myform").submit(function(e) {
            var searchedValue = $("input[name='searchedValue']").attr("value");
            if (searchedValue) {
                window.location.href = ""+searchedValue;
            } else {
                alert("empty string");

var searchedValue = $("input[name='searchedValue']").val() 

you should use val() instead of attr(). Rest of the things are fine.

Javascript Redirect window.location.href Not Working · Issue #86 , I'd like to make an auto redirect if some condition is true. following " window.​location.href " in one of the end chain file does not work. reached here window.​location.href = ''; alert("not reached here. true; } else { if​( window.location.href = url ){ clearTimeout(X); return true; }else{  @Helloise Smit: When you use window.location.href the browser always does a GET request. If you want that, you can add the value of the textarea to the URL in the same manner as mailid, using the encodeURIComponent function to encode the value first.

<form id="myform">
        <input type="search" name="searchedValue">
        <input type="submit" value="Szukaj" onclick="search(event)">
    function search(event) {
        // [0] gets the first textbox of current page with name.
        var searchedValue = document.getElementsByName('searchedValue')[0].value;
        if (searchedValue && event) {
            event.preventDefault(); // cancels the event if it is cancelable
            var specs = "height=auto,width=auto";
            var searchUrl = "" + searchedValue;
  , "_blank", specs);
        } else {
            alert("empty string");

Useful links like and preventDefault(). I think it's better to practice with pure javascript and DOM manipulations when you are learning. Anyway, keep up the hard work. :)

Can't get window.location.href to work correctly - JavaScript, I have this file (below) after submitting it gives an alert then suppose to redirect. The problem is the redirect url is missing the trailing slash  It does not work :( the check status button always return null. On you can see it works. I do not know yet how.You click th fb button, opens a popup. And when it close, the opener window redirects. – elranu May 12 '11 at 3:06

Code is fine, i think your browser is blocking your redirect to"+searchedValue;

It may happen when you have https website and want to redirect to http. Console will silently display error regarding insecure redirect.

JavaScript: location.href to open in new window/tab?, How do I make Windows href open in a new tab? window.location.href or simply, location.href to change the page in the window, not document.location. document.location (now deprecated in favour of document.url) is not the same as window.location. document.location is supposed to be read only. However, Netscape (and it appears FF) allow this property value to be changed.

HTML, ). on('click','span. external-link',function(){ var t = $(this), URL = t. window location href and location reload() both redirects a page to a new page but there are some difference to follow. Here is the difference between window.location.href and location.reload: window.location.href is not a method. It is a property which will tell you the current URL location of the browser.

[Solved] JavaScript document.location.href not working , value point to another website or point to an email address. Unfortunately, it always shows 5 windows whether alert is showing or not. I know 1 is for main window, 1 is for status bar, the other 3 windows I have no idea. The 3rd attempt is to check that underlying elements (behind alert) can't be touched, which is to use isHittable. This property does not work, it always returns true. Check the content

JavaScript Window Location, function confirmsave() { var ans = confirm('Do u want to Save?'); if (ans == true) {​document.location.href='login.aspx',true} else { return false; } } I found this issue in a website and I thought it would be nice to share the info. If you search “window.location = window.location.pathname” in Google, you will see some people are using this method for redirection purposes. This can make a website vulnerable to Open Redirect issue though.

  • Use .val instead of .attr
  • This is code from that question, the value doesent matter it is displayed correctly in alert i have added: So im not uderstand 3 pluses here.
  • val() and attr("value") do the same !
  • There is a slight difference between these two and in your fiddle, if you will upgrade the jQuery version you will etc the error.
  • Thank you very much, that was the issue apparently.
  • Simply you can change url to https to make it work i think.
  • He says that in every scenario the alert pops up instead of running the actual window.location.href. The problem here is the if statement, not the url.
  • This ic code from that question, the value doessent matter it is displayed correctly in alert: The only problem i can se may be related with thing i have added in this post.