jsonp原生跨域

本文阅读 0 分钟
首页 前端笔记 正文
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <link href="https://cdn.bootcss.com/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
  9. </head>
  10. <body>
  11. <div id="box">
  12. <form id="form" action="" class="form-inline" target="_blank">
  13. <div class="form-group">
  14. <input type="text" class="form-control" id="search" placeholder="search" autocomplete="off">
  15. </div>
  16. <button type="submit" class="btn btn-default" id="btn">search</button>
  17. </form>
  18. <ul class="list-group" id="list" style="width: 260px;">
  19. </ul>
  20. </div>
  21. <script>
  22. function loadScript(url) {
  23. loadScript.mark = 'load';
  24. var script = document.createElement('script');
  25. script.type = 'text/javascript';
  26. script.src = url;
  27. document.body.appendChild(script);
  28. }
  29. function callback(data) {
  30. if (data) {
  31. var arr = data.s;
  32. var html = '';
  33. for (var i = 0; i < arr.length; i++) {
  34. html += "<li class='list-group-item'><a href='https://www.baidu.com/s?wd=" + arr[i] + "' target='_blank'>" + arr[i] + "</a></li>";
  35. }
  36. list.innerHTML = html;
  37. }
  38. }
  39. search.onkeyup = function(e) {
  40. e = e || event;
  41. e.preventDefault();
  42. console.log(e);
  43. if (e.keyCode == '13') {
  44. window.open('https://www.baidu.com/s?wd=' + this.value);
  45. }
  46. if (this.value) {
  47. console.log(search.data);
  48. if (search.data != this.value) {
  49. form.setAttribute('action', 'https://www.baidu.com/s?wd=' + this.value);
  50. var that = this;
  51. loadScript('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + that.value + '&cb=callback')
  52. }
  53. } else {
  54. list.innerHTML = '';
  55. }
  56. search.data = this.value;
  57. }
  58. search.onclick = function(e) {
  59. e = e || event;
  60. list.style.display = 'block';
  61. if (e.stopPropagation) {
  62. e.stopPropagation();
  63. } else {
  64. e.cancelBubble = true;
  65. }
  66. }
  67. document.onclick = function() {
  68. list.style.display = 'none';
  69. }
  70. </script>
  71. </body>
  72. </html>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP打乱数组,保持键值对关系
« 上一篇 10-12
canvas绘制中国国旗
下一篇 » 10-17

发表评论