zoukankan      html  css  js  c++  java
  • jsonp原生js跨域拿新浪数据插件封装【可扩展】

    //修改了一个bug,增加了手动释放垃圾

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>jsonp跨域源码插件封装</title>
    </head>
    
    <body>
      <input type="text" id="code">
      <input type="button" value="查询" id="serch">
      <script>
        ; (function (win) {
          const jsonp = function (dataPack) {
            const callback = dataPack.callBack || '';
            const code = dataPack.code || 'sh000001';
            const getUrl = 'http://hq.sinajs.cn/list=' + code;
            let scriPt = document.createElement('script');
            scriPt.src = getUrl;
            scriPt.id = 'xl';
            const oldScript = document.getElementById('xl');
            const success = function () {
              const val = eval('hq_str_' + code);
              //手动垃圾回收
              for (let i in window) {
                if (/^hq_str_/.test(i)) {
                  window[i] = null;
                }
              }
              callback && callback(val);
            }
            const error = function () {
              callback && callback('跨域发生了错误!');
            }
            if (oldScript) {
              document.body.removeChild(oldScript);
              document.body.appendChild(scriPt);
            } else {
              document.body.appendChild(scriPt);
            }
    
            scriPt.onload = success;
            scriPt.onerror = error;
          }
          win.jsonp = jsonp;
        })(window);
        document.querySelector('#serch').addEventListener('click', function () {
          jsonp({
            code: document.querySelector('#code').value,
            callBack: function (data) {
              console.log(data);
            }
          });
        })
      </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    627. whose 和 who's
    628. why 和why not
    629 will: 各种用法tyg
    enChapter 3 Underlying Technologiesp
    使用VIEWER.JS进行简单的图片预览
    outlook2010设置失败后重新设置
    新增和编辑clob字段
    金钱大写
    pivot 与 unpivot 函数是SQL05新提供的2个函数
    从函数到委托
  • 原文地址:https://www.cnblogs.com/webSong/p/9418828.html
Copyright © 2011-2022 走看看