[英]Code to redirect a page and open a new tab?
基本上我想要做的是设置一个代码,如果按下转义键,它将打开一个新页面 (weather.com) 并将当前页面重定向到 google.com。
function main() {
(document).on("keydown", function(event) {
if (event.which = 27) {
window.open ("https://weather.com/", "_newtab")
window.location.replace ("https://www.google.com/")}
}
);
}
当我运行当前代码时,没有任何反应。 我对 Javascript 和 Tampermonkey 很陌生,所以不知道我做错了什么。 任何人都可以帮忙吗?
有三个错误。
function main() {
document.addEventListener('keydown', function (event) {
if (event.which === 27) {
window.open('https://weather.com/', '_newtab')
window.location.replace('https://www.google.com/')
}
});
}
main();
这样可以解决问题。
function main(){
document.onkeydown = function(event) {
if (event.which == 27) {
window.open ("https://weather.com/", "_newtab")
window.location.replace ("https://www.google.com/")}
}
}
}
main();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.