admin管理员组

文章数量:1336089

I'm trying to set up a variable in a domain (like my-site) which I can acess from other site (like my-other-site). Basically, when I receive a response from my server, I want to set a variable in the localStorage and then redirect the page to the next site (e.g: in my-site I set the variable 'foo' and redirect the page. Inside my-other-site I would like to access that variable (foo)). Oh! I almost forget, I'm using Vue

I already tried to use iframe, using this tutorial (). I tried some libraries like cross-storage () and biforst-cors() but these both didn't work as I expected. I know that is possible to use query params, but I can't use it know

I have this:

localStorage.clear()
window.localStorage.setItem('user', JSON.stringify(response.body.data) )                                                
window.localStorage.setItem('token', response.body.token)                                                
window.location.href = `http://localhost:8081/#/` //redirect 

Basically, I would like to user both variables user and token in other domain.

If someone could help me bringing ideias or something like that, It would help me a lot.

I'm trying to set up a variable in a domain (like my-site.) which I can acess from other site (like my-other-site.). Basically, when I receive a response from my server, I want to set a variable in the localStorage and then redirect the page to the next site (e.g: in my-site. I set the variable 'foo' and redirect the page. Inside my-other-site. I would like to access that variable (foo)). Oh! I almost forget, I'm using Vue

I already tried to use iframe, using this tutorial (https://blog.teamtreehouse./cross-domain-messaging-with-postmessage). I tried some libraries like cross-storage (https://www.npmjs./package/cross-storage#overview) and biforst-cors(https://www.npmjs./package/bifrost-cors) but these both didn't work as I expected. I know that is possible to use query params, but I can't use it know

I have this:

localStorage.clear()
window.localStorage.setItem('user', JSON.stringify(response.body.data) )                                                
window.localStorage.setItem('token', response.body.token)                                                
window.location.href = `http://localhost:8081/#/` //redirect 

Basically, I would like to user both variables user and token in other domain.

If someone could help me bringing ideias or something like that, It would help me a lot.

Share Improve this question asked Sep 23, 2019 at 18:13 Karl HarderKarl Harder 511 gold badge1 silver badge3 bronze badges 4
  • You can't do anything directly to some other domain. You need to municate between them on the server, or use postMessage() if one page has a reference to a window or iframe containing the other. – Barmar Commented Sep 23, 2019 at 18:58
  • But, can I use it when the page (the page witch I want to use those variables) is loading? – Karl Harder Commented Sep 23, 2019 at 19:05
  • 1 No, you can't. Every domain has its own localStorage. When you redirect to another domain, you have no access to the previous domain's data. – Barmar Commented Sep 23, 2019 at 19:16
  • I see, thanks for the ment :). I'll try to find a way to do this without queries params. – Karl Harder Commented Sep 25, 2019 at 16:57
Add a ment  | 

2 Answers 2

Reset to default 1

A different solution would be this:

var DifferentWindow=window.open("http://www.example./");
DifferentWindow.localStorage.setItem("Key","Value");

This is in simple Javascript, so I hope it helps What it does is open a new window with the link, and set a variable inside of that window's local storage to "value."

Solution with Bifrost-cors lib. https://www.npmjs./package/bifrost-cors

You have to initialize lib in both domains.
on exampleA.
var bifrostCors = new bifrostCors("http://exampleB./")
on exampleB.
var bifrostCors = new bifrostCors("http://exampleA./")

then you can use
bifrostCors.setLocalStorage({key: "user", value: "persons"})

本文标签: javascriptSet a localStorage in a different domainStack Overflow