我已尝试过以下脚本的许多变体,包括更改语法和使用window.prompt,但无法找到提示工作的方法。
注意:如果我的其他代码(html)中有错误,请随意指出它们,但要专注于JS - 页面与所有元素完美地加载,但脚本根本不能在移动设备上运行设备,即使它在我没有提示时运行完美。你能帮我吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>program</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script>
var accesskey="config";
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera
Mini/i.test(navigator.userAgent) ) {
var attempt=window.prompt("Mobile browsers are not currently supported. If
you are a developer, enter the access key.")
if(accesskey!=attempt)
{
alert("Bye!");
window.location("https://google.com);
}
else
{
console.log("Authenticated");
}
}
</script>
</body>
</html>
答案 0 :(得分:2)
prompt
消息中包含多个换行符/空格。window.location
不是一个功能;只需为其分配URL。"
末尾错过了结束google.com
。修复这三个问题会产生以下工作示例:
var accesskey = "config";
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
var attempt = window.prompt("Mobile browsers are not currently supported. If you are a developer, enter the access key.")
if (accesskey != attempt) {
alert("Bye!");
window.location = "https://google.com";
} else {
console.log("Authenticated");
}
}
希望这有帮助! :)
答案 1 :(得分:1)
提示工作如下regex
有换行符也是假的,所以我强迫它在这里是真的。
另外window.location("https://google.com);
缺少关闭"
并且其属性不是您应该执行的功能window.location="https://google.com"
var accesskey="config";
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || true)
var attempt=window.prompt("Mobile browsers are not currently supported. If you are a developer, enter the access key.")
if(accesskey!=attempt)
{
alert("Bye!");
window.location ="https://google.com";
}
else
{
console.log("Authenticated");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>program</title>
</head>
<body>
</body>
</html>