JavaScript cross-domain implementation

Source: Internet
Author: User

Recently a service that uploads files, which contains an uploaded Web page. The goal is to have the client page Nest this web page and then upload the file directly to the server.

Because the file is different, you need to save the folder name is not the same, so the client needs to pass a folder name, tell the server to save the target location, which involves the parameters of the IFrame pass: clientàserver

After the file upload is successful, the server returns the file name or related information to the client for other operations. This also involves one parameter pass: server->client

The above is a large summary of the upload tool. But the notes I made were mainly about the problem of parameter passing. Here I am involved in the JavaScript cross-domain problem, my implementation in two cases, there are two ways to implement (there are many examples and instructions on the Internet for cross-domain)

first Case : primary domain is the same, subdomain is different

For example: A.baidu.com and b.baidu.com

The key is to use the document.domain= "ceshi.com" statement, which is added to both the client page and the server side page.

1Document.domain = "ceshi.com";//when the primary domain is the same and the subdomain is different2$ ("#mainFrame"). Load (function () {3             varClientHeight = $ ("#mainFrame"). Contents (). Find ("Body"). Height ();4             varClientWidth = $ ("#mainFrame"). Contents (). Find ("Body"). width ();5$( This). Height (clientheight);6$( This). Width (clientwidth);7$ ("#mainFrame"). Contents (). Find ("Input[id=hiddenid]"). Val ("100"); 8              //The primary domain is the same, and the subdomain does not pass a parameter ID9});

The second case : The primary domain is not the same, the domain name is different

For example: Www.baidu.com and www.sohu.com

This situation is more troublesome, I use Window.location.hash, the client needs to add a proxy page.

Delivery principle: Use #currentfolder=### when the client home page is called #传递参数

Server-side pages are received and saved to an input hidden using the following code

1 //The following are different when the primary domain2       vardata = {};3         varHash_url;4         functionDealhash () {5Hash_url = Window.location.hash;//Get to Parameters6             varDatas = Hash_url.split ("#") [1].split ("&"));7              for(vari = 0; i < datas.length; i++) {8                 vart = datas[i].split ("=");9Data[t[0]] = decodeuricomponent (t[1]);Ten             } One         } A  -         functionChange () { -             if(Hash_url! =Window.location.hash) { the Dealhash (); -document.getElementById ("FolderName"). Value = data["CurrentFolder"]; -             } -}

When the upload succeeds and then calls the callback method to pass the file name, which is exactly the same as the client passing parameters to the server side, but this message to the client agent. This makes it easier for the client to communicate with the Agent page and home page.

1 function Callback (data) {2             var iframe = document.getElementById ("MainFrame"); 3             var New Date (). GetTime (); 4             iframe.src = (url + "#filename =" + data); 5         }

The implementation of the proxy page:

WINDOW.PARENT.PARENT.ADD (document.getElementById ("Hiddenid"). Value); This is the Add method that executes the home page:

JavaScript cross-domain implementation

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.