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?

Share Improve this question edited Feb 26, 2021 at 23:10 sideshowbarker 88.6k30 gold badges215 silver badges212 bronze badges asked Feb 26, 2021 at 21:25 jesse rurkajesse rurka 311 silver badge2 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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?

Share Improve this question edited Feb 26, 2021 at 23:10 sideshowbarker 88.6k30 gold badges215 silver badges212 bronze badges asked Feb 26, 2021 at 21:25 jesse rurkajesse rurka 311 silver badge2 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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

本文标签: