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

html - JavaScript window.location.href not working in localhost - Stack Overflow

programmeradmin0浏览0评论

I have a problem with my website. When there is "333" in the input box (id textbox_text) website is supposed to redirect to the specified url, but instead nothing happens. Where is the problem?

    <script>
            function IsEmpty()
            {
            if(document.forms['frm'].textbox_text.value == "333")
             {
                window.location.href="martyna-lesniak.html";
            }
            else 
            {
                alert("Nieprawidłowe hasło!");
            }
        }
    </script>
    <div class="container">
        <form name="frm">
            <input type="password" name="password1" class="password-input" id="textbox_text" placeholder="Wpisz hasło">
            <input type="submit" name="submit" onclick="return IsEmpty();" value="Wysyłaj" class="button-input" />
        </form>
    </div>

I have a problem with my website. When there is "333" in the input box (id textbox_text) website is supposed to redirect to the specified url, but instead nothing happens. Where is the problem?

    <script>
            function IsEmpty()
            {
            if(document.forms['frm'].textbox_text.value == "333")
             {
                window.location.href="martyna-lesniak.html";
            }
            else 
            {
                alert("Nieprawidłowe hasło!");
            }
        }
    </script>
    <div class="container">
        <form name="frm">
            <input type="password" name="password1" class="password-input" id="textbox_text" placeholder="Wpisz hasło">
            <input type="submit" name="submit" onclick="return IsEmpty();" value="Wysyłaj" class="button-input" />
        </form>
    </div>
Share Improve this question asked Apr 26, 2020 at 1:11 ForcowiczForcowicz 1081 gold badge1 silver badge10 bronze badges 2
  • Use window.location.replace(url). Href only works on click – Socrates Tuas Commented Apr 26, 2020 at 1:14
  • Still nothing :/ Page just refreshes – Forcowicz Commented Apr 26, 2020 at 1:30
Add a ment  | 

3 Answers 3

Reset to default 6

You just have to return false; after your call to window.location.href="martyna-lesniak.html";.

If you have the HTML that you need to refer in other folder that the actual html you need to put the path of the resource (html) the other thing is in the conditional you can put === to have a literal equal object

Try this

<script>
        function IsEmpty()
        {
        if(document.forms['frm'].textbox_text.value == "333")
         {

            window.location.href="martyna-lesniak.html";
            return false;
        }
        else 
        {
            alert("Nieprawidłowe hasło!");
        }
    }
</script>
<div class="container">
    <form name="frm">
        <input type="password" name="password1" class="password-input" id="textbox_text" placeholder="Wpisz hasło">
        <input type="submit" name="submit" onclick="return IsEmpty();" value="Wysyłaj" class="button-input" />
    </form>
</div>
发布评论

评论列表(0)

  1. 暂无评论