开发者

passing javascript global variable between embeded pages or from main to embed

开发者 https://www.devze.com 2023-03-25 07:46 出处:网络
i have a html page with 2 embed pages <body> <embed src =\"toolbar.html\" type=\"application/xhtml+xml\"width=\"485px\" height=\"60px\">

i have a html page with 2 embed pages

<body>
    <embed src ="toolbar.html" type="application/xhtml+xml"  width="485px" height="60px">
    <embed src="editor.svg" type="image/svg+xml" width="595px" height="842px">
</body>

the first is a toolbar (jquery ui) the second is an xml page with svg in it. both pages have scripts. Is there any way to pass a variable defined in a script from the toolbar to the svg开发者_如何转开发 ?

or if that isn't possible:

is there any way to pass a variable defined in the current document to an embedded page ?

tnx in advance


  1. Turn the embed elements into iframe elements, don't forget the close tags.

    The W3 - Technical Review - HTML 5 - The embed element mentions

    The embed element represents an integration point for an external (typically non-HTML) application or interactive content.

    so you should rather use iframe elements instead of embed elements, which mentions:

    The iframe element represents a nested browsing context.

    in which a browsing context is defined as:

    A browsing context is an environment in which Document objects are presented to the user.

  2. You can declaring a global variable in a script you use in the main page.

    It is as simple as var myvariable;

  3. You can access the variable from within an iframe using window.parent.myvariable.

    The DOM object window.parent is defined as such:

    Returns a reference to the parent of the current window or subframe.

    If a window does not have a parent, its parent property is a reference to itself.

    When a window is loaded in an <iframe>, <object>, or <frame>, its parent is the window with the element embedding the window.

    A shortened version is to use parent.myvariable, as window is the root DOM object.

  4. Make sure that the domains, protocols and ports of the different documents match.

    Unless you want to be presented with a nasty error like:

    Unsafe JavaScript attempt to access frame with URL X from frame with URL Y.
    Domains, protocols and ports must match.

0

精彩评论

暂无评论...
验证码 换一张
取 消