我在互联网上找到了这个代码:
javascript:location.href="googlechrome"+location.href.substring(4);
如果该代码放在Safari中的书签中,它将打开Chrome浏览器并加载用户在Safari中的当前网页 .
但我想要的是当用户点击书签时,在Chrome中打开之前,选项卡将在Safari中关闭 . 这有可能吗?
您可以使用 window.close() 关闭当前选项卡 .
window.close()
javascript:location.href="googlechrome"+location.href.substring(4); window.close();
请注意,如果您的网址开始使用_367615而不是 http:// ,则解析网址的方式将无效 .
http://
这是一个使用URI.js用googlechrome替换当前URL中的协议的示例,这应该比仅使用 location.href.substring(4) 更可靠 .
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>
1 回答
您可以使用
window.close()
关闭当前选项卡 .javascript:location.href="googlechrome"+location.href.substring(4); window.close();
请注意,如果您的网址开始使用_367615而不是
http://
,则解析网址的方式将无效 .这是一个使用URI.js用googlechrome替换当前URL中的协议的示例,这应该比仅使用
location.href.substring(4)
更可靠 .