1

I have an html page (A) that submits a form using JavaScript with a target of _blank so the new page (B) opens in a new window. After some additional processing of undetermined time, Page A needs to tell Page B something. Page B sees Page A as its opener and as such, I can reference functions in Page A from B.

However, I would like a JavaScript function in Page A to call a JavaScript function in Page B. How do I reference a function in Page B from Page A?

Velocedge
  • 1,222
  • 1
  • 11
  • 35
  • 1
    Use sockets (long pooling) and connect via server listening. Respond to server requests. Marking as too broad. – Justinas Oct 21 '16 at 11:47
  • 1
    maybe instead of having separate pages, just create a (modal) popup using CSS then everything is in one page. That way you avoid the problem entirely, but the user gets basically the same experience – ADyson Oct 21 '16 at 11:56

1 Answers1

0

You cannot actually reference functions from other tabs, there is no perfect way to achieve this, but few work arounds can get it done. Although the chrome extensions dev allows tabs to communicate with each other if that is your case, else check out below, the ways which I feel may work

1: Using cookies check out this SO answer Instead of what the answer has used to send and receive. You can send(set cookies) as function names. If you want to keep them in sync from time to time and keep on updating them this is best way. Else if its a one time thing check out way 2

2.Another work around would be to submit the form along with the function names and time after which the functions in page B be needs to be called. so accordingly you can take the QUERY PARAMETERS and www.page.com/pageA when a form here is submitted by may be POST/GET append the query parameters to the PAGE B link www.page.com/pageB?f=function_to_be_called|200 Now when pageB loads treat the QUERY PARAMETERS and call them accordingly.

3.This is a huge workload and will only be helpful if large chunks need to be exchanged, use sockets to communicate to your server and let the pageA send some data to you server, parse it and send the fruitful data to pageB which also has kept an open socket. Checkout socket.io

Think before you work around with these. If this situation is really needed then go for it else let users browse in the same page. May be provide them with a modal. Writing scripts to set cookies from console and fooling the pageB in many ways can be done easily.Let me know if you make any other way or if this helps.

Community
  • 1
  • 1
Girish
  • 154
  • 1
  • 6
  • We have to support all major browsers so I'm not sure the Chrome extensions approach would work. I'm actually using SignalR in Page A and was wanting to avoid having to add that to Page B every time. But that would work. Since Page A is Page B's opener, I'm also considering polling Page A to check for updates but I hate paging. – Velocedge Oct 21 '16 at 13:52
  • On your option two, I don't know the time because it will vary with the load. I know the function name in Page A, I just don't when to call it... back to polling! ;-) – Velocedge Oct 21 '16 at 13:54