現在的瀏覽器越來越多,這就使得網頁設計的兼容性要求越來越高,像常用的設為首頁、加入收藏代碼,一般的雖然簡潔,但兼容性卻非常不好,加入收藏和設為首頁代碼幾乎各個網站都在頭部放上一個,有沒有效果先不管,需求應該是有的。
但是由于瀏覽器的兼容性問題,之前用的很多代碼都失去效果,下面就給出一段能夠兼容各個瀏覽器的代碼,也不能夠算是兼容,只能說在不支持的瀏覽器中能夠給出提示,代碼如下:
<!doctype html><html><head><title>加入收藏和設為首頁</title><script type="text/javascript">//加入收藏function AddFavorite(sURL, sTitle){sURL = encodeURI(sURL); try{ window.external.addFavorite(sURL, sTitle); }catch(e){ try{ window.sidebar.addPanel(sTitle, sURL, ""); }catch(e){ alert("加入收藏失敗,請使用Ctrl+D進行添加,或手動在瀏覽器里進行設置.");} }}//設為首頁function SetHome(url){if (document.all){document.body.style.behavior='url(#default#homepage)';document.body.setHomePage(url);}else{alert("您好,您的瀏覽器不支持自動設置頁面為首頁功能,請您手動在瀏覽器里設置該頁面為首頁!");}} </script></head><body><a href="javascript:void(0)">設為首頁</a><a href="javascript:void(0)">加入收藏</a></body></html>
以上代碼簡短易懂,可以兼容各個瀏覽器加入收藏和設為首頁,有問題歡迎提出,小編會及時和大家聯系的,謝謝!
下面給大家分享一段jquery代碼實現加入收藏夾功能
//設為首頁function SetHomePage() { if (document.all) { document.body.style.behavior = 'url(#default#homepage)'; document.body.setHomePage('http://www.87cool.com'); } else if (window.sidebar) { if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("該操作被瀏覽器拒絕,如果想啟用該功能,請在地址欄內輸入 about:config,然后將項 signed.applets.codebase_principal_support 值該為true"); } } var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); prefs.setCharPref('browser.startup.homepage', 'http://www.87cool.com'); }}//加入收藏夾function AddFavorite() { var title = document.title; var url = location.href; if (window.sidebar) { window.sidebar.addPanel(title, url, ""); } else if (document.all) { window.external.AddFavorite(url, title); } else { return true; }}
新聞熱點
疑難解答