0

In my application, i need to open a popup and display some results coming from a Javascript function. I open the popup window with the command:

var popup=window.open('popup.html', 'width=500', 'height=500');

where popup.html is an html page that calls the chart.js library for showing some graphs. I need to pass data to this window in order to display the correct graph. I tried a lot of examples, but none worked. How could i solve this problem?

Luca

Jimmy Page
  • 71
  • 8
  • Pass info in #hash or ?search. Your parameters are wrong too `var popup=window.open('popup.html#parmtopass', 'windowName', 'width=500,height=500');` – mplungjan May 23 '16 at 07:38
  • @anu - not really a great duplicate – mplungjan May 23 '16 at 07:39
  • If the pages are on the same domain, save the JSON in a global var and access it from the child as `opener.jsonvar` - or create a getter and call it: `opener.getJsonVar()` – mplungjan May 23 '16 at 07:50
  • This seems an interesting answer, thanks, i'll give it a try. – Jimmy Page May 23 '16 at 07:53
  • I tried with a simple global string and with a simple function that gives a simple string, calling them with `opener.variable` / `opener.gimmeSomeText()`, the graphic does not load. – Jimmy Page May 23 '16 at 08:05
  • when i try to read a global variable in the child window using`var text=window.opener.nameOfGlobalVariable`: Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match. – Jimmy Page May 23 '16 at 08:36
  • So the pages are loaded from the file system, not from a server? – mplungjan May 23 '16 at 08:42
  • I'm so stupid, i was trying it locally. However, now the popup fully loads, but the global variable is displayed as 'Undefined'. No errors in the console. – Jimmy Page May 23 '16 at 08:58
  • can you in parent page do `console.log("parent:",nameOfGlobalVariable)` and in child window: `console.log("child:",opener.nameOfGlobalVariable)` – mplungjan May 23 '16 at 09:31
  • This global variable seems to be Undefined in the child and in the parent too. The var is: `var JSONob={ labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "38", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52"], datasets: [{ label: 'xxx', backgroundColor: "rgba(123,234,254,0.5)", data: [100, 10, 10, 10] }, ... , ]}` – Jimmy Page May 23 '16 at 09:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112665/discussion-between-mplungjan-and-jimmy-page). – mplungjan May 23 '16 at 09:49

1 Answers1

1

You could pass basic data in the url hash, change your JS to

var popup=window.open('popup.html#MY_DATA', 'myWindow','width=500,height=500');

and then in the popup.html page you can access the it by looking into the location.hash variable:

window.location.hash
//This will be "#MY_DATA"
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
  • http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip – mplungjan May 23 '16 at 07:39
  • So i can simply declare a `var text="someLongText";` in my JS function, pass it with the `'popup.html#text'` parameter of the `window.open` function, and use it in a script conteined in the popup.html page, with a line like `var text2=window.location.hash;`? – Jimmy Page May 23 '16 at 07:45
  • No, you'd have to pass it as 'popup.html#someLongText' or 'popup.html#'+text – Joe Thomas May 23 '16 at 07:46
  • @mplungjan Thanks, generally I don't really care in my code, but thanks anyway. – Joe Thomas May 23 '16 at 07:46
  • Ok, got it. The problem is that the someLongText is a really long JSON object. Since i didn't know this possibility of parameter passing, there is some limit on the length of the passed string? – Jimmy Page May 23 '16 at 07:49
  • That'd depend on the browser, most can handle up to 100 thousand characters. So you'll probably be fine. – Joe Thomas May 23 '16 at 07:51
  • Stay under 2K though – mplungjan May 23 '16 at 07:52
  • Yea, IE is weird like that. – Joe Thomas May 23 '16 at 07:53