我在网上找到这段代码:
javascript:location.href="googlechrome"+location.href.substring(4);
如果将该代码放入 Safari 的书签中,它将打开 Chrome 浏览器并加载用户在 Safari 中的当前网页。
但我想要的是当用户单击书签时,该选项卡将在 Safari 中关闭,然后再在 Chrome 中打开。有这种可能吗?
最佳答案
您可以使用window.close()
关闭当前选项卡。
javascript:location.href="googlechrome"+location.href.substring(4); window.close();
但请注意,如果您的网址以 https://
而不是 http://
开头,则解析网址的方式将不起作用。
这是一个使用 URI.js 的示例将当前 URL 中的协议(protocol)替换为 googlechrome,这应该比仅使用 location.href.substring(4)
更可靠。
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.7.2/URI.min.js"></script>
<script>
function closeSafariAndOpenChrome()
{
var chromeProtocol = 'googlechrome';
var redirectedUrl = URI(location.href)
.protocol(chromeProtocol);
location.href = redirectedUrl;
window.close();
}
</script>
</head>
<body>
<a href="javascript:closeSafariAndOpenChrome();">Click here to open chrome</a>
</body>
</html>
关于javascript - 在 Chrome 中打开 url 并关闭 Safari 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503199/