0

I think this question has been asked, but I've searched through Google and here, and still a bit confused on how it works.

I have two scripts, CS1.coffee and CS2.coffee, CS1 is what is loaded with the main page, and has a button on it. When the button is pressed a window appears and CS2 is run. However, I want to refer to variables that exist in CS1 from methods in CS2.

In other terms,

  1. Main page loads, calls CS1 (which has button logic)
  2. Button is pressed
  3. A new KendoWindow appears which calls CS2 (this window displays data that is taken from variables in CS1)

Sample Code:

CS1.coffee
root = export ? this
root.number = 42
$("#winButton").click=->
    //new kendoWindow appears, with content being a separate html containing CS2 scripts
root.testFunc =->
    alert 'I was called'

CS2.coffee
root = export ? this
answer = root.number
$("#testButton").click =->
    root.testFunc
CBredlow
  • 2,790
  • 2
  • 28
  • 47
  • Are those variables in CS1 global variables? – Šime Vidas Nov 07 '12 at 15:15
  • If both windows have the same domain, they can access each others global variables normally. (If window 1 opened window 2, you will have a reference to window 1 from within window 2.) Otherwise, there are methods like cross-document messaging which enable the windows to pass data via messages. – Šime Vidas Nov 07 '12 at 15:16
  • @Šime I've added in some sample code that might explain what I'm doing, I think my exports are wrong. I'm still very new to javascript/coffeescript/web stuff and I looked at http://stackoverflow.com/questions/4214731/coffeescript-global-variables for reference on global variables. – CBredlow Nov 07 '12 at 15:22
  • Two windows *don't* share the same global (window) object. In CS2, you have to retrieve the window object of the first window. Did you use `window.open()` to open the second window? – Šime Vidas Nov 07 '12 at 15:40
  • No, it's something like this (using Kendo windows) win = ("#window"); win.kendoWindow {width:"250px", title:"new window", content:"newWindow.html"} – CBredlow Nov 07 '12 at 15:43
  • Wait, it's not an actual browser window, but just a DIV on the same page which acts as a dialog box? Do you have two separate HTML documents (files), or is the second page just a DIV element within the first page? – Šime Vidas Nov 07 '12 at 15:49
  • They are two separate html files – CBredlow Nov 07 '12 at 15:56
  • ... and the HTML source code of `newWindow.html` is injected into the `
    ` element on the first page? In that case both scripts *do* share the same global object.
    – Šime Vidas Nov 07 '12 at 15:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19234/discussion-between-cbredlow-and-sime-vidas) – CBredlow Nov 07 '12 at 15:59

0 Answers0