判断浏览器环境

  1. function getSystem(){
  2. const mac = /mac/i,
  3. linux = /linux/i,
  4. win = /win/i;
  5. const platform = navigator.platform.toLowerCase();
  6. if(mac.test(platform)){
  7. return 'MAC';
  8. } else if(win.test(platform)){
  9. return 'WIN';
  10. } else if(linux.test(platform)){
  11. return 'Linux';
  12. }
  13. return undefined;
  14. }
  15. const browser = {
  16. versions:function(){
  17. let ret = 'xxSys';
  18. const u = navigator.userAgent;
  19. const isMobile = !!u.match(/AppleWebKit.*Mobile.*/),
  20. ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
  21. android = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  22. if(isMobile){
  23. if(ios) return 'IOS';
  24. if(android) return 'Android';
  25. } else {
  26. ret = getSystem() || ret;
  27. }
  28. return ret;
  29. }(),
  30. };
  31. export default browser;

使用

  1. import browser from "../util/browers.js"
  2. console.log(browser.versions);

加入收藏夹

  1. function addFavorite(sURL, sTitle) {
  2. try {
  3. window.external.addFavorite(sURL, sTitle);
  4. } catch (e) {
  5. try {
  6. window.sidebar.addPanel(sTitle, sURL, "");
  7. } catch (e) {
  8. alert("加入收藏失败,请使用Ctrl+D进行添加");
  9. }
  10. }
  11. }