Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13873 > unrolled thread

Variable scope between <script>

Started byScott Johnson <noonehome@chalupasworld.com>
First post2012-05-09 18:16 -0700
Last post2012-05-10 04:19 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Variable scope between <script> Scott Johnson <noonehome@chalupasworld.com> - 2012-05-09 18:16 -0700
    Re: Variable scope between <script> Andreas Bergmaier <andber93@web.de> - 2012-05-10 13:19 +0200
      Re: Variable scope between <script> Scott Johnson <noonehome@chalupasworld.com> - 2012-05-10 05:16 -0700
      Re: Variable scope between <script> Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-10 19:26 +0200
    Re: Variable scope between <script> Richard Cornford <Richard@litotes.demon.co.uk> - 2012-05-10 04:19 -0700

#13873 — Variable scope between <script>

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-05-09 18:16 -0700
SubjectVariable scope between <script>
Message-ID<jof4tq$asa$1@dont-email.me>
I am working with opencart which is templated.

On the foot template file there is a music script (jplayer) located on a 
separate file 'music.html' loaded into a iframe on the footer, which is 
global across all pages.

The client would only like the jplayer to autoplay on the home page so I 
need a way to determine when the home page is called so I can play or 
pause the player in the jplayer script located on 'music.html'.

I tried to call window.location.href ? .pathname which sends back the 
original name of the file the script is located '/~domain/music.html' as 
expected but not helpful.

What I was thinking was place the window.location.pathname on the 'home' 
page set a global variable which can be read by the script on music.html.

Is this even possible?
Global variables accessible across scripts.
or
Do I even make sense? :)

Thanks

[toc] | [next] | [standalone]


#13874

FromAndreas Bergmaier <andber93@web.de>
Date2012-05-10 13:19 +0200
Message-ID<jog879$arn$1@news.albasani.net>
In reply to#13873
Scott Johnson schrieb:

> I tried to call window.location.href ? .pathname which sends back the
> original name of the file the script is located '/~domain/music.html' as
> expected but not helpful.
>
> What I was thinking was place the window.location.pathname on the 'home'
> page set a global variable which can be read by the script on music.html.
>
> Is this even possible?
> Global variables accessible across scripts.

You can access the global object of the parent window with "parent", 
even the global object of the top window with "top" for deeply nested 
frames. Precondition is that both pages are located in the same domain 
for security reasons, which will be no problem in your case.

So you may check parent.location.href, but you can also set up a 
function in the homepage that calls the play() function in the frame by 
accessing the frame's global object.

regards,
  Bergi

[toc] | [prev] | [next] | [standalone]


#13875

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-05-10 05:16 -0700
Message-ID<jogbik$1n0$1@dont-email.me>
In reply to#13874
On 5/10/2012 4:19 AM, Andreas Bergmaier wrote:
> Scott Johnson schrieb:
>
>> I tried to call window.location.href ? .pathname which sends back the
>> original name of the file the script is located '/~domain/music.html' as
>> expected but not helpful.
>>
>> What I was thinking was place the window.location.pathname on the 'home'
>> page set a global variable which can be read by the script on music.html.
>>
>> Is this even possible?
>> Global variables accessible across scripts.
>
> You can access the global object of the parent window with "parent",
> even the global object of the top window with "top" for deeply nested
> frames. Precondition is that both pages are located in the same domain
> for security reasons, which will be no problem in your case.
>
> So you may check parent.location.href, but you can also set up a
> function in the homepage that calls the play() function in the frame by
> accessing the frame's global object.
>
> regards,
> Bergi

Ahhh.

parent & top

That makes sense.  I will give that some experimentation.

Thanks for the reply.  That is very good info to know.

Scotty

[toc] | [prev] | [next] | [standalone]


#13876

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-05-10 19:26 +0200
Message-ID<1401679.AFRqVoOGUt@PointedEars.de>
In reply to#13874
Andreas Bergmaier wrote:

> Scott Johnson schrieb:
>> I tried to call window.location.href ? .pathname which sends back the
>> original name of the file the script is located '/~domain/music.html' as
>> expected but not helpful.
>>
>> What I was thinking was place the window.location.pathname on the 'home'
>> page set a global variable which can be read by the script on music.html.
>>
>> Is this even possible?
>> Global variables accessible across scripts.
> 
> You can access the global object of the parent window with "parent",
> even the global object of the top window with "top" for deeply nested
> frames.

`parent' and `top' are actually properties of Window objects.

<http://docs.oracle.com/cd/E19957-01/816-6408-10/window.htm>

> Precondition is that both pages are located in the same domain
> for security reasons, which will be no problem in your case.

Without further measures, it must be the same protocol, the same domain, and 
the same port number (Same Origin Policy).  It may also be possible if both 
protocol and port number match and the URIs used to access both resources 
are in the same subdomain (simplest case: same second-level domain).  I am 
not sure if assigning to `document.domain' still works in recent browsers.  
I have not tried again; no use-case.  But it would certainly be tested as 
part of a DOM Support Matrix.

<https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript>
<http://docs.oracle.com/cd/E19957-01/816-6408-10/document.htm#1193628>

> So you may check parent.location.href, […]

The `href' property of Location objects was tainted, the `location' property 
of Window objects was not.  So, slightly greater efficiency aside, it 
appears to be safer and more flexible to use the `location' property 
instead.  It appears to have a getter which returns the value of the `href' 
property of the referred Location object.

<http://docs.oracle.com/cd/E19957-01/816-6408-10/window.htm#1202507>


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#13931

FromRichard Cornford <Richard@litotes.demon.co.uk>
Date2012-05-10 04:19 -0700
Message-ID<45b68143-bf1f-48e9-b998-ec38f095892e@w10g2000vbc.googlegroups.com>
In reply to#13873
On May 10, 2:16 am, Scott Johnson wrote:
> I am working with opencart which is templated.

Saying that isn't directly useful as you are probably asking people to
look up "opencart" (assuming that there is only one), which asking for
more than you are likely to get (if its relevant you should provide
the reference), and you have not explained what "templated" means in
this context, or what implications that may have in this context.

> On the foot template file there is a music script (jplayer)
> located on a separate file 'music.html' loaded into a iframe
> on the footer,

That sounds slightly mad, design wise (referring to the iframe use,
playing music at a web site visitor is antisocial but does serve to
reflect informatively on worth of the web site's owner).

> which is
> global across all pages.
>
> The client would only like the jplayer to autoplay on the home page

Because generating a bad first impression is so important?

> so I
> need a way to determine when the home page is called so I can
> play or pause the player in the jplayer script located on
>'music.html'.

So why not put the code that plays the music in the home page mark-up
and not in the footer mark-up where it will serve no purpose most of
the time?

> I tried to call window.location.href ? .pathname which sends
> back the original name of the file the script is located
> '/~domain/music.html' as expected but not helpful.

No, you would want the URL of the parent page that contains the IFRAME
(i.e. - parent.location.href -).

> What I was thinking was place the window.location.pathname on
> the 'home' page set a global variable which can be read by the
> script on music.html.

So you can place scripts on that page, and so can run the music player
from there?

> Is this even possible?

Yes, but if you don't understand that each frame will have its own
global environment (so one for the main page and a separate one for
each frame/iframe it contains (and possibly so on recursively) then
you will have as much trouble accessing a global variable defined in
the parent page as you have accessing the - location - object of the
parent page.

> Global variables accessible across scripts.

Yes, but that is the wrong question in context.

> or
> Do I even make sense? :)

I tend to think that there is a minimal level of technical
understanding that should be achieved before it is practical to make
informed design decisions. It is hard to see not having an
understanding of the (navigation of the) nested frame structure that
browsers provide as qualifying.

Richard.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web