开发者

Variable scoping between HTML file, Javascript file, and PHP file echo statements

How can I define a global javascript variable in this situation? I want the variable to be accessible from three places:

  1. the HTML page - I want to be able to reference the variable in the code in the HTML page favorites.php.
  2. the javascript file - In the HTML page I load a javascript file called portal.js with
  3. a PHP file - In the HTML file I load loadtreeb.php in an iframe. In loadtreeb.php I have echo javascript code and want to reference that variable in that code.

Here is a scenario.

In the HTML, favorites.php:

<head>
    <script type="text/javascript">
       var d = new dTree();
    </script>
</head>
<body>
        <iframe id="tnav" src="loadtreeb.php" style="border: 0px; width: 200px; height: 86%; overflow: auto;">
        </iframe>
</body

In the javascript file, portalb.js:

function deleteitem() {
     d.s(1);
     d.setCookie('csd', '1');
}

In the php file, loadtreeb.php:

echo '<script type="text/javascript">';

/* echo '   d = new dTree(\'d\');'; */

 echo "d.add('1','-1','My Favorites','#','','','images/root.gif','images/rootopen.gif','1');";

 echo  '    document.write(d);';

 echo 'd.s(0);';

 echo  '</script>';

In this scenario, I attempt to define the variable d in the header section of the HTML page, then I reference it in three places without redefining that variable. This doesn't do it. The javascript statements echoed in the loadtreeb.php file don't seem to know about the variable. They did when the variable was defined in the echo statements. But开发者_如何转开发 now I must access the same variable elsewhere. So I commented out the declaration statements and put it in the HTML script tag.

What is the way to achieve the accessiblity that I want in all of those places for that variable?


  1. the HTML page - I want to be able to reference the variable in the code in the HTML page favorites.php.
  2. the javascript file - In the HTML page I load a javascript file called portal.js with

Executing your javascript on the page is the same as having it directly in the page ( so it will access variable on the page on wich you execute it ( and that are before in the code ) ).

The title is not quite correct, it would be exactly the same issue with only 2 html page ( and one in an iframe on the other ).

So here if it's on the same domain, you can just use :

parent.window.d

In the iframe to use the var in the parent frame.

If you had wanted to use a var inside the iframe from the parent frame, you would have to do :

window.frames["tnav"].d


An iframe is (for the most part) a completely separate page with its own Javascript context. Thus, variables that you define in the main page will not normally be visible to the Javascript context of the iframe and vice versa.


You're going to have problems accessing data across frames. It's possible, but nasty. You'll probably want to do something like define some getters and setters on the browser's top object, which will necessitate some changes to the JS being generated by the PHP script. It's going to be unpleasant.


In the favorites.php JS you should declare top.d = new dTree();

In the loadtreeb.php JS, you must use cross-window access: top.d.whatever or parent.d.whatever

If you need to go down the frame tree then top.frames[0].window or similar.

See https://developer.mozilla.org/en/DOM/window.frames

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜