How can I define a global javascript variable in this situation? I want the variable to be accessible from three places:
- the HTML page - I want to be able to reference the variable in the code in the HTML page favorites.php.
- the javascript file - In the HTML page I load a javascript file called portal.js with
- 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?