JavaScript implementation JSONP

Source: Internet
Author: User

Jsonp principle is also simple, although Ajax cannot cross-domain, but through the SRC attribute we can achieve cross-domain, in fact, we introduce a third party jquery call it the same way.

Html:

<! DOCTYPE html>

JS:

<script> function Jsonp (data) {var Oul = document.getelementsbytagname (' ul ') [0];var aLi = Oul.getelementsbytag Name (' Li '), if (data.s.length) {var htmltext = '; oUl.style.display = ' block '; for (var i = 0; i < data.s.length; i++) {htm Ltext + = ' <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.no Dename) 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 ';d ocument.body.appendChild (oscript); }}}else{var oscript = document.createelement (' script '); oscript.src = ' http://suggestion.baidu.com/su?wd= ' + This.value+ ' &cb=jsonp ';d ocument.body.appendChild (Oscript);}} </script>

  

JavaScript implementation JSONP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.