demo.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 防劫持
  2. (function() {
  3. var href = window.self.location.href;
  4. if (window.frameElement) {
  5. (window.top || window.parent).location.href = href;
  6. return;
  7. };
  8. // var site = window.self.location.protocol + '//' + window.self.location.hostname;
  9. // try {
  10. // if (window.top.location.href !== href) {
  11. // window.top.location.href = site + '/404.html';
  12. // };
  13. // } catch(e) {
  14. // window.self.location.href = site + '/404.html';
  15. // };
  16. })();
  17. /**
  18. * 代码高亮
  19. */
  20. hljs.initHighlightingOnLoad();
  21. // 导航高亮当前位置
  22. (function() {
  23. // console.log(typeof document.querySelector)
  24. if (typeof document.querySelectorAll !== 'function') {return};
  25. var hrefs = {
  26. full: location.href,
  27. path: location.href
  28. };
  29. if (hrefs.path.indexOf('?') >= 0) {
  30. hrefs.path = hrefs.path.slice(0, hrefs.path.indexOf('?'));
  31. };
  32. var links = document.getElementById('nav').querySelectorAll('a');
  33. var _link;
  34. var _index;
  35. // console.log(hrefs);
  36. for (var i = 0, l = links.length; i < l; i++) {
  37. _link = links[i].href;
  38. if (_link === hrefs.full) {
  39. _index = i;
  40. break;
  41. };
  42. if (_link.indexOf('?') >= 0) {
  43. _link = _link.slice(0, _link.indexOf('?'));
  44. };
  45. if (_link === hrefs.path) {
  46. _index = i;
  47. break;
  48. };
  49. };
  50. if (typeof _index === 'number' && _index >= 0) {
  51. links[_index].parentNode.classList.add('n');
  52. };
  53. })();