admin管理员组文章数量:1026455
I have access to the <head>
tag on both pages and I need to send data from one page to another. Both pages are on different domains.
Page A domain (www.foo)
Page B domain (www.bar)
If I declared a variable, say var test_parameter = 'something';
... How would I get that variable from one page to the next using some sort of code in the <head>
tag?
I have access to the <head>
tag on both pages and I need to send data from one page to another. Both pages are on different domains.
Page A domain (www.foo.)
Page B domain (www.bar.)
If I declared a variable, say var test_parameter = 'something';
... How would I get that variable from one page to the next using some sort of code in the <head>
tag?
- You either need to post to a server script that enables CORS, or you have to put the value in a URL query parameter. – Barmar Commented Feb 26, 2021 at 21:26
2 Answers
Reset to default 4You can use Window.postMessage
as long as one page opened the other.
Page A (https://example)
var test_parameter = 'something';
Window.open('https://example')
.postMessage(test_parameter, 'https://example');
Page B (https://example)
window.addEventListener('message', (event) => {
// Do not do anything unless the message was from
// a domain we trust.
if (event.origin !== 'https://example') return;
// Create a local copy of the variable we were passed.
var test_parameter = event.data;
// Do something...
// Optionally reply to the message (Page A must also have
// a 'message' event listener to receive this message).
event.source.postMessage('Done!', 'https://example');
}, false);
You can use in www.foo.
var parameterValue = "something";
window.location = "www.bar.?parameter="+parameterValue;
And in www.bar.
var parUrl = window.location.search;
var urlParams = new URLSearchParams(parUrl);
var parameter = urlParams.get('parameter') // something
I have access to the <head>
tag on both pages and I need to send data from one page to another. Both pages are on different domains.
Page A domain (www.foo)
Page B domain (www.bar)
If I declared a variable, say var test_parameter = 'something';
... How would I get that variable from one page to the next using some sort of code in the <head>
tag?
I have access to the <head>
tag on both pages and I need to send data from one page to another. Both pages are on different domains.
Page A domain (www.foo.)
Page B domain (www.bar.)
If I declared a variable, say var test_parameter = 'something';
... How would I get that variable from one page to the next using some sort of code in the <head>
tag?
- You either need to post to a server script that enables CORS, or you have to put the value in a URL query parameter. – Barmar Commented Feb 26, 2021 at 21:26
2 Answers
Reset to default 4You can use Window.postMessage
as long as one page opened the other.
Page A (https://example)
var test_parameter = 'something';
Window.open('https://example')
.postMessage(test_parameter, 'https://example');
Page B (https://example)
window.addEventListener('message', (event) => {
// Do not do anything unless the message was from
// a domain we trust.
if (event.origin !== 'https://example') return;
// Create a local copy of the variable we were passed.
var test_parameter = event.data;
// Do something...
// Optionally reply to the message (Page A must also have
// a 'message' event listener to receive this message).
event.source.postMessage('Done!', 'https://example');
}, false);
You can use in www.foo.
var parameterValue = "something";
window.location = "www.bar.?parameter="+parameterValue;
And in www.bar.
var parUrl = window.location.search;
var urlParams = new URLSearchParams(parUrl);
var parameter = urlParams.get('parameter') // something
本文标签:
版权声明:本文标题:Sending data from one domain to another using javascript (I have access to the <head> of both web pages) - Stack O 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745639837a2160676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论