SantinoDu
SantinoDu

Reputation: 43

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'.

Upvotes: 0

Views: 128

Answers (1)

gurvinder372
gurvinder372

Reputation: 68413

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

Upvotes: 1

Related Questions