Javascript重定向不适用于移动设备

问题描述 投票:1回答:1

我有一个Javascript代码(下面),通过在新选项卡中打开,将用户重定向到我的网站的主页。我遇到的问题是它无法在移动设备上运行。

function redirect(url) {
var win = window.open(url);
win.focus();
}

当我单击应该打开新选项卡的按钮时,没有任何反应,但它可以在Opera和Chrome的桌面上运行。

有谁知道如何解决这个问题,以便它可以在移动设备上运行?

编辑:根据请求添加HTML

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<script src="login.js"></script>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>

<h2>Modal Login Form</h2>

<button onclick="document.getElementById('id01').style.display='block'"
    style="width: auto;">Login</button>

<div id="id01" class="modal">

    <form class="modal-content animate" method=POST>
        <div class="imgcontainer">
            <span onclick="document.getElementById('id01').style.display='none'"
                class="close" title="Close Modal">&times;</span> <img
                src="loginAvatar.png" alt="Avatar" class="avatar">
        </div>

        <div class="container">
            <label><b>Username</b></label> <input type="text"
                placeholder="Enter Username" name="username" id="uname" required>
            <script>
                var suppliedUsername = document.getElementById("uname").value;
            </script>

            <label><b>Password</b></label> <input type="password"
                placeholder="Enter Password" name="psw" id = "pword" onChange="verify(document.getElementById('uname').value, document.getElementById('pword').value)" required>
            <script>
                var password = document.getElementById("pword").value;
            </script>
            <button id="loginbutton" onClick="verify(document.getElementById('uname').value, document.getElementById('pword').value)">Login</button>
            <input type="checkbox" checked="checked"> Remember me
        </div>

        <div class="container" style="background-color: #f1f1f1">
            <button type="button"
                onclick="document.getElementById('id01').style.display='none'"
                class="cancelbtn">Cancel</button>
            <span class="psw">Forgot <a href="#">password?</a></span>
        </div>
    </form>
</div>

<script>
    // Get the modal
    var modal = document.getElementById('id01');

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
</script>
</body>
</html>
javascript html redirect web
1个回答
1
投票

只需使用链接。如果需要,可以将其设置为带有CSS的按钮。

<a href="url" target="_blank">Text</a>`

为什么你必须使用花哨的Javascript做一些你可以通过一个简单的链接做的事情?

如果您需要在Javascript代码中触发它,请查看this post。或者,如果你使用jQuery(我认为你是),看看here

© www.soinside.com 2019 - 2024. All rights reserved.