Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variable scope between <script>

6 views
Skip to first unread message

Scott Johnson

unread,
May 9, 2012, 9:16:20 PM5/9/12
to
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

Andreas Bergmaier

unread,
May 10, 2012, 7:19:12 AM5/10/12
to
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

Richard Cornford

unread,
May 10, 2012, 7:19:12 AM5/10/12
to
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.

Scott Johnson

unread,
May 10, 2012, 8:16:01 AM5/10/12
to
Ahhh.

parent & top

That makes sense. I will give that some experimentation.

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

Scotty

Thomas 'PointedEars' Lahn

unread,
May 10, 2012, 1:26:50 PM5/10/12
to
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
0 new messages