Just for recording some points casually.

Items:

  • links
  • Animation/Transition optimize with animationend/transitionend event.
  • 特殊字符串转义、编解码
  • Ajax XMLHttpRequest
  • 保存网页中对象
  • a 标签下载文件
  • 模拟按键

links

When target="_blank", remember to add rel="noopener"

特殊字符串转义、编解码

  1. HTMLDecode: function (text) {
  2. var temp = document.createElement("div");
  3. temp.innerHTML = text;
  4. var output = temp.innerText || temp.textContent;
  5. temp = null;
  6. return output;
  7. }

Ajax XMLHttpRequest

包括简单封装、请求头等参数

  1. function httpRequest(config) {
  2. var xhr = new XMLHttpRequest();
  3. xhr.onload = function() {
  4. if (xhr.readyState === 4 && xhr.status === 200) {
  5. config.callback(xhr.response);
  6. }
  7. };
  8. xhr.open(config.type, config.url, true);
  9. xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  10. xhr.send(config.body);
  11. }

改进

  1. const sendXMLHttpRequest = config => {
  2. const xhr = new XMLHttpRequest();
  3. xhr.open(config.method, config.url);
  4. xhr.setRequestHeader('content-type', 'application/json');
  5. xhr.onload = function() {
  6. if (xhr.status >= 200 && xhr.status < 300) {
  7. config.onSuccess(xhr.response);
  8. } else {
  9. config.onError(xhr.response);
  10. }
  11. };
  12. xhr.send(JSON.stringify(config.body));
  13. };
  14. // 用例
  15. sendXMLHttpRequest({
  16. method: 'POST',
  17. url: '/api/admin/guard',
  18. body: {
  19. url: url,
  20. },
  21. onSuccess: response => {
  22. console.log('sendToUser success: ', response);
  23. message.success('名额发放成功');
  24. },
  25. onError: response => {
  26. try {
  27. const res = JSON.parse(response);
  28. console.log('sendToUser failed: ', res);
  29. message.error(`名额发放失败:${res.message || res.err}`);
  30. } catch (e) {
  31. console.log(e);
  32. }
  33. },
  34. });

奇怪的 header

X-Request: JSON

X-Requested-With: XMLHttpRequest

保存网页中对象

参考 Snippets 中 SaveObjInPage

  1. fileName = 'c.json';
  2. x = app.page.pins;
  3. x = JSON.stringify(x, null, 2);
  4. var blob = new Blob([x], {type: 'text/json'}),
  5. e = document.createEvent('MouseEvents'),
  6. a = document.createElement('a');
  7. a.download = fileName;
  8. a.href = window.URL.createObjectURL(blob);
  9. a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
  10. e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  11. a.dispatchEvent(e);

a 标签下载文件

  1. function download(href, filename = '') {
  2. const a = document.createElement('a');
  3. a.download = filename;
  4. a.href = href;
  5. document.body.appendChild(a);
  6. a.click();
  7. a.remove();
  8. }
  9. // 考虑跨域, 兼容性升级版
  10. function downloadFile(url, fileName) {
  11. var x = new XMLHttpRequest();
  12. x.open('GET', url, true);
  13. x.responseType = 'blob';
  14. x.onload = function (e) {
  15. var blobUrl = window.URL.createObjectURL(x.response);
  16. download(blobUrl, fileName)
  17. window.URL.revokeObjectURL(blobUrl);
  18. };
  19. x.send();
  20. }

参考 浅析 HTML5 中的 download 属性

模拟按键

  1. let mockKeyboardEvent = new KeyboardEvent('keydown', { ctrlKey: true });
  2. if (darkMode) {
  3. document.dispatchEvent(mockKeyboardEvent);
  4. document.dispatchEvent(mockKeyboardEvent);
  5. document.dispatchEvent(mockKeyboardEvent);
  6. }