zoukankan      html  css  js  c++  java
  • javascript 实现jsonp

    jsonp原理其实也简单,虽然ajax不能跨域,但是通过src这个属性我们可以实现跨域,其实和我们引入第三方jquery调用它的方法一样的。

    html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>jsonp</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <script>
        function jsonp(data){
        	var oUl = document.getElementsByTagName('ul')[0];
    		var aLi = oUl.getElementsByTagName('li');
    		if(data.s.length){
    			var htmlText = '';
    			oUl.style.display = 'block';
    			for(var i  = 0 ; i < data.s.length; i++){
    				htmlText += '<li><a target="_blank" href="http://www.baidu.com/s?wd='+data.s[i]+'">'+data.s[i]+'</a></li>';
    			}
    			oUl.innerHTML = htmlText
    		}else{
    			oUl.style.display='none'
    		}
        }
        document.onclick = function(e){
        	console.log(e.target.nodeName)
        	if(e.target.nodeName.toLowerCase() != 'input'){
        		document.getElementsByTagName('ul')[0].style.display = 'none'
        	}
        }
    	window.onload = function(){
    		var oInput = document.getElementById('search');
    		oInput.oninput = function(){
    			var that= this;
    			var scriptTag = document.getElementsByTagName('script');
    			var tempScript =[];
    			var reg = /http://suggestion.baidu.com/su?wd=(.*)&cb=jsonp/;
    			if(scriptTag.length>1){
                    for(var i=0 ;i<scriptTag.length;i++){
                    	if(scriptTag[i].src && reg.test(scriptTag[i].src)){
                            document.body.removeChild(scriptTag[i]);
                            var oScript = document.createElement('script');
    						oScript.src = 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=jsonp';
    						document.body.appendChild(oScript);
                    	}
                    }
    			}else{
    				var oScript = document.createElement('script');
    				oScript.src = 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=jsonp';
    				document.body.appendChild(oScript);
    			}
    		}
    	}
    </script>
    <style>
    	input{
    		200px;
    		height:40px;
    		line-height: 40px;
    		text-indent: 5px;
    		outline:none;
    		border:1px solid #333;
    	}
    	ul{
    		list-style: none;
    		padding:0px;
    		margin:0px;
    		200px;
    		border:1px solid #333;
    		display: none;
    	}
    	ul li{
    		padding: none;
    		margin:0px;
    
    
    
    	}
    	ul li:hover{
    		background:#f1f1f1;
    	}
    	ul li a{
    		text-decoration: none;
    		color:#333;
    		height:40px;
    		display: block;
    		overflow: hidden;
    		text-overflow: ellipsis;
    		white-space: nowrap;
    		100%;
    		line-height: 40px;
    	}
    </style>
    </head>
    <body>
        <input type="text" id="search">
        <ul>
        </ul>
    </body>
    </html>
    

    js:

    <script>
        function jsonp(data){
        	var oUl = document.getElementsByTagName('ul')[0];
    		var aLi = oUl.getElementsByTagName('li');
    		if(data.s.length){
    			var htmlText = '';
    			oUl.style.display = 'block';
    			for(var i  = 0 ; i < data.s.length; i++){
    				htmlText += '<li><a target="_blank" href="http://www.baidu.com/s?wd='+data.s[i]+'">'+data.s[i]+'</a></li>';
    			}
    			oUl.innerHTML = htmlText
    		}else{
    			oUl.style.display='none'
    		}
        }
        document.onclick = function(e){
        	console.log(e.target.nodeName)
        	if(e.target.nodeName.toLowerCase() != 'input'){
        		document.getElementsByTagName('ul')[0].style.display = 'none'
        	}
        }
    	window.onload = function(){
    		var oInput = document.getElementById('search');
    		oInput.oninput = function(){
    			var that= this;
    			var scriptTag = document.getElementsByTagName('script');
    			var tempScript =[];
    			var reg = /http://suggestion.baidu.com/su?wd=(.*)&cb=jsonp/;
    			if(scriptTag.length>1){
                    for(var i=0 ;i<scriptTag.length;i++){
                    	if(scriptTag[i].src && reg.test(scriptTag[i].src)){
                            document.body.removeChild(scriptTag[i]);
                            var oScript = document.createElement('script');
    						oScript.src = 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=jsonp';
    						document.body.appendChild(oScript);
                    	}
                    }
    			}else{
    				var oScript = document.createElement('script');
    				oScript.src = 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=jsonp';
    				document.body.appendChild(oScript);
    			}
    		}
    	}
    </script>
    

      

  • 相关阅读:
    最小的k个数
    数组中出现次数超过一半的数字
    字符串的排列
    二叉搜索树与双向链表
    复杂链表的复制
    二叉树中和为某一值的路径
    centos7安装wrk
    【胡思乱想】JNI与线程池的维护
    【胡思乱想】命令模式中,命令对象如何解耦Invoker和Receiver
    【胡思乱想】命令模式 与 Thread Runnable
  • 原文地址:https://www.cnblogs.com/bestsamcn/p/5425861.html
Copyright © 2011-2022 走看看