最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Sending data from one domain to another using javascript (I have access to the <head> of both web pages) - Stack O

matteradmin7PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far