Declare shared variable

Discussion in 'JavaScript' started by harish_dixit1, Sep 21, 2006.

  1. #1
    Hello to all,

    I want o declare a shared variable in javascript file so that other intsance
    of application can also use it.

    Is there any method to define such shared variable?
     
    harish_dixit1, Sep 21, 2006 IP
  2. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hi Harish,

    Do you mean that if a person opens 2 browsers, both pointing to your site, the variable should be shared between the browsers? If so, you can't do this in javascript.

    If you mean, declare a variable in one .js file so it is useable in another .js file, then just declare the variable globally (not within a function) and the variable will be available to both files.
     
    BurgerKing, Sep 21, 2006 IP
  3. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi!

    BurgerKing is right: Javascript environments are restricted to one page. All Javascript coded included in a page operates within that one environment. Access to other JS environments in the user agent is not permitted, for security reasons, and the fact it's not part of the spec.

    Regards
    - P
     
    penagate, Sep 27, 2006 IP
  4. aashish757

    aashish757 Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi all,
    I am searching for a thing like shared variable,I read in the forum that in javascript shared variable concept is not there, but i want to share variable among multiple tabs of one instance.
    please help me on this.


    Thanks in advance.
     
    aashish757, Apr 8, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    aashish757, there's two ways to handle messages between tabs & windows.

    For the one that supports tabs from different domains you'll have to keep an eye on each browsers support of HTML5 & whether or not they'll be including support for the cross document messaging spec. this article seems to suggest Opera has it implemented already.




    If you want to share data from tabs on the same domain, they must trace back to a single opened tab. The application must start with a single tab & that tab must open the subsequent tabs.

    For instance if you want to open a tab & have that new tab tell the tab which opened it that it opened successfully you could do something like this in the initial tab.
    var success = function(url)
    {
      alert(url);
    }
    var sub_tab = self.open('new_tab.html');
    Code (markup):
    In "new_tab.html" you would have this.
    self.opener.success(self.location);
    Code (markup):
    The function from the original tab should be called & will alert you to the URL of the new sub tab doing the calling.

    One thing you must be aware of is that in some browsers, noteably Firefox, if the visitor opens the new tab via their right-click menu and selecting "open in new tab" instead of just clicking the link & letting the script open it in a new tab, the bond will be severed & comunication between the two tabs will not be possible.
     
    joebert, Apr 8, 2008 IP