简体   繁体   中英

how to get cross-domain javascript response text

I have a big size js in domain a.exmaple.com, in page b.example.com need this js.

As the js size is 300kb more, I want to use localStorage to storage it.

Now is the problem, I can't get the js text, in page b.exmaple.com, I use a script tag link the js,so the script.innerHTML is ''.

If I use the xhr get the response.text, it will cross domain.

And it's may hard make the js set response header Access-Control-Allow-Origin:'b.example.com'.

Rather than avoiding download of js files, try to enable deflate options in your web server

If this is not an option, you can structure your js file as a JSON, something like

function classname(){}
classname.method = function(){}..

Now once you have downloaded, simply do JSON.stringify(className) and store it in your localstorage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM