...

View Full Version : javascript scope



firepages
05-20-2012, 08:59 AM
I have a popup in one of my pages which is populated via AJAX call with a list of click-able options, this is all good, however I am now trying to display a javascript tree in the popup but it can not seem to 'see' the javascript routine that creates the tree, even if I hard code that into the popup itself, so the tree starts with d=new dTree() , and javasript is telling me that 'd' is undefined.

the code works fine in a regular webpage but when pulled up in a AJAX generated popup it cant see squat? I tried parent.dTree() etc but to no avail.

Is this because the javascript is loaded after the main page load via AJAX? if so is it accessible in anyway ?

chump2877
05-20-2012, 10:15 AM
In Firebug, do you see the AJAX-generated javascript? If so, can you paste it here? In fact, just paste everything source-related that you can find inside Firebug.

firepages
05-20-2012, 12:33 PM
hey cheers , yes I can, did not have firebug installed... now even more confused cos its all there...



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<body>
<center>
<div id="message_stick" style="position:absolute;top:100px;left:280px;width:500px;height:100px;border: 2px solid #696969;background-color:white;visibility:hidden;text-align:center;">
<div id="centaur_popup" class="centaur_popup_as_usersel" style="position: absolute; display: block; visibility: visible;">
here
<script src="http://srv.blah.com.au/js/dtree.js" type="text/javascript">
<div id="region_tree">
<script type="text/javascript">
d = new dTree('d');
d.add(0,-1,'Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=1','','');
d.add(1,0,'Tech Subcriptions','?FPA_TASK=fpa_regions_admin&_ctl_edit=3','','');
d.add(2,1,'Skype','?FPA_TASK=fpa_regions_admin&_ctl_edit=2','','');
d.add(3,0,'Server Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=4','','');
d.add(4,3,'testserver2','?FPA_TASK=fpa_regions_admin&_ctl_edit=6','','');
d.add(5,3,'blah.com','?FPA_TASK=fpa_regions_admin&_ctl_edit=5','','');
document.write(d);
</script>
<a href="javascript: d.openAll();">open all</a>
|
<a href="javascript: d.closeAll();">close all</a>
</div>
</div>
<table style="width:980px;">
<br>
<small></small>
</center>
</body>
</html>


now if I copy and paste the above into a regular HTML file , it works just fine, in the popup it shows me only the open and close links which if I click debugger says ...

Timestamp: 20/05/12 19:27:40
Error: d is not defined
Source File: javascript:%20d.closeAll();
Line: 1

firepages
05-20-2012, 01:11 PM
weird thing is, I put a function testit() which just alerts a message in the dtree.js file and added an onclick in the ajax generated content and that works fine, e.g. testit()
works



<div id="region_tree">
<a href="javascript: testit();">test</a> << HERE
<script type="text/javascript">
d = new parent.dTree('d');
d.add(0,-1,'Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=1','','');
d.add(1,0,'Tech Subcriptions','?FPA_TASK=fpa_regions_admin&_ctl_edit=3','','');
d.add(2,1,'Skype','?FPA_TASK=fpa_regions_admin&_ctl_edit=2','','');
d.add(3,0,'Server Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=4','','');
d.add(4,3,'testserver2','?FPA_TASK=fpa_regions_admin&_ctl_edit=6','','');
d.add(5,3,'blah.com.au','?FPA_TASK=fpa_regions_admin&_ctl_edit=5','','');
document.write(d);
</script>



this is the dtree.js btw ... http://destroydrop.com/javascripts/tree/example/dtree.js

chump2877
05-20-2012, 01:33 PM
so this line of code:


<script src="http://srv.blah.com.au/js/dtree.js" type="text/javascript">

is not a part of the ajax-generated content?

chump2877
05-20-2012, 01:37 PM
the problem might be:


document.write(d);

try attaching the new content to your page via the DOM instead of via document.write()

chump2877
05-20-2012, 01:45 PM
also, where is the DOM object with id 'd' that corresponds to this statement:


d = new dTree('d');

..perhaps the element doesn't exist or has not loaded yet

firepages
05-20-2012, 02:13 PM
so this line of code:


<script src="http://srv.blah.com.au/js/dtree.js" type="text/javascript">

is not a part of the ajax-generated content?

no, its already loaded on the initial page, though I have tried loading it within the ajax-gen content instead and also just adding the entire script into the ajax-gen content as well with no joy.

d is just the returned object ID I assume , like $d=new $class

.. i dont know enouigh about javascript to know how to get the content out of 'd' to do a DOM innerHTML call

firepages
05-20-2012, 02:17 PM
again please note that the content shown above cut and pasted into a HTML page works without issue

chump2877
05-20-2012, 02:28 PM
i meant this 'd':


d = new dTree('d');

firepages
05-20-2012, 02:38 PM
ah ok, thats just a unique id, so you could have several trees , d1=new dTree('d1'), etc

chump2877
05-21-2012, 12:05 PM
You are missing a closing script tag here:


<script src="http://srv.blah.com.au/js/dtree.js" type="text/javascript">

I was troubleshooting the JS instead of the markup. You really should make sure that your markup is valid. It's pretty goofy in here:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<body>
<center>
<div id="message_stick" style="position:absolute;top:100px;left:280px;width:500px;height:100px;border: 2px solid #696969;background-color:white;visibility:hidden;text-align:center;">
<div id="centaur_popup" class="centaur_popup_as_usersel" style="position: absolute; display: block; visibility: visible;">
here
<script src="http://srv.blah.com.au/js/dtree.js" type="text/javascript">
<div id="region_tree">
<script type="text/javascript">
d = new dTree('d');
d.add(0,-1,'Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=1','','');
d.add(1,0,'Tech Subcriptions','?FPA_TASK=fpa_regions_admin&_ctl_edit=3','','');
d.add(2,1,'Skype','?FPA_TASK=fpa_regions_admin&_ctl_edit=2','','');
d.add(3,0,'Server Logins','?FPA_TASK=fpa_regions_admin&_ctl_edit=4','','');
d.add(4,3,'testserver2','?FPA_TASK=fpa_regions_admin&_ctl_edit=6','','');
d.add(5,3,'blah.com','?FPA_TASK=fpa_regions_admin&_ctl_edit=5','','');
document.write(d);
</script>
<a href="javascript: d.openAll();">open all</a>
|
<a href="javascript: d.closeAll();">close all</a>
</div>
</div>
<table style="width:980px;">
<br>
<small></small>
</center>
</body>
</html>

firepages
05-21-2012, 03:39 PM
yeah thats just a copy from firebug without the tree expaned to keep it short.. I will put something online and post back.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum