![](/img/trans.png)
[英]Javascript why Product2 is never called contrary to Product1
[英]How to allow my Product1 users to bookmark my Product2 link from Product1 web page?
我試圖讓我的用戶通過單擊按鈕來為我的外部產品鏈接添加書簽,但是 window.sidebar.addPanel 和 window.external.AddFavorite 出現問題,因為現在兩者都不支持。
我有一個 PHP 應用程序並在這里尋找最合適的解決方案。
我收到“Uncaught TypeError: window.sidebar.addPanel is not a function”錯誤,代碼如下。
<head>
<script type="text/javascript">
function AddToBookmark () {
if (window.sidebar) { // Firefox
window.sidebar.addPanel ('Dottoro help page', 'http://help.dottoro.com', '');
}
else {
if (window.external && ('AddFavorite' in window.external)) {
// Internet Explorer
window.external.AddFavorite ('http://help.dottoro.com', 'Dottoro help page');
}
else { // Opera, Google Chrome and Safari
alert ("Your browser doesn't support this example!");
}
}
}
</script>
</head>
<body>
<button onclick="AddToBookmark();">Add to Bookmark</button>
</body>
根據這個答案
addPanel
已從 Firefox 版本 23 中刪除。但您可以使用標記代替:<a href="http://stackoverflow.com" title="Stack Overflow" rel="sidebar">Bookmark me</a>
因此,您可以在AddToBookmark()
函數中模擬對類似<a>
標簽的點擊,如下所示。
const anchorTag = document.createElement('a');
anchorTag.href = "http://help.dottoro.com"
anchorTag.title= "Dottoro help page";
anchorTag.rel= "sidebar";
anchorTag.click();
<body> <button onclick="AddToBookmark();">Add to Bookmark</button> <script type="text/javascript"> function AddToBookmark() { if (window.sidebar) { // Firefox const anchorTag = document.createElement('a'); anchorTag.href = "http://help.dottoro.com" anchorTag.title = "Dottoro help page"; anchorTag.rel = "sidebar"; anchorTag.click(); } else { if (window.external && ('AddFavorite' in window.external)) { // Internet Explorer window.external.AddFavorite('http://help.dottoro.com', 'Dottoro help page'); } else { // Opera, Google Chrome and Safari alert("Your browser doesn't support this example!"); } } } </script> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.