Browser cross-domain access to LRC lyrics data solution _ajax related

Source: Internet
Author: User
Obtaining data from the same domain can be achieved by XMLHTTP components or IFRAME, and there are no permissions issues with Cross-domain access, so it is simpler.
However, if you want to access data from different domains, because of browser security settings, XMLHTTP does not have access to the data, and the IFRAME does not have the right to pass the acquired data to the parent window, there seems to be no other solution.

Epiphany......
Scripts that refer to different fields within a Web page do not prompt for insufficient permissions, yes, that's right!

The solution has been found, now to test it briefly:

First create a new page (test.html) on the 51js.com server.

<textarea id="runcode32456"><ptml> <pead> <title> over-domain Data fetching Test page</title> <script type= "Text/jav Ascript "> var lastscript; var h=document.getelementsbytagname ("head") [0]; function Loadscript (URL) {var f=document.createelement ("script"); var d=new Date (). GetTime (); F.type= "Text/javascript"; F.id=d; f.src=url+ '? ' +d; H.appendchild (f); if (Lastscript) g (Lastscript). Parentnode.removechild (g (Lastscript)); Lastscript=d; function g (x) {return document.getElementById (x)}; </script> </pead> <body> <button onclick= "loadscript (' http://aboutplayer.com/alert.js ')" > Test alert</button> <button onclick= "loadscript (' http://aboutplayer.com/info.js ')" >get my info</ button> my Name: <input id= "myname" type= "text" value= "" > My Blog: <input id= "MyBlog" type= "text" value= "" > </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Explain two key points:
1. Lastscript is used to hold the ID of the last established script node, and the next time you want to create a new script node, delete the last established node to prevent more and more scripts from loading and consuming too much memory.
2. The URL followed by a value d is to prevent browser caching script data, in this case can not be added, but if the script is dynamically generated by the server, it is best to add.

Then create two java script scripts for testing.

Alert.js:
Alert (' You can me! ');
Info.js:
G (' MyName '). Value= ' spectrum music audio-visual ';
G (' MyBlog '). value= ' www.aboutplayer.com ';

Well, the music has put them under the aboutplayer.com server so that they and test.html are not in the same domain.

Open the test.html file in a browser.
Click "Test Alert" and a dialog box should be displayed to show you the can see me!.
Click "Get me Info" to display the information in the text box.

Further, if the LRC lyrics data into the JS script file (not directly change the file name extension), you can achieve Cross-domain access to LRC lyrics data.

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.