View Full Version : document.body.appendChild return value

03-05-2012, 04:08 PM
I'm not a JS programmer, I stumble around.
I have a web page that uses:

d = document.createElement("script");
d.type = "text/javascript";
d.src = "/var/www/html/UpDist.php";
return (document.body.appendChild(d)); <-- what I want to do

UpDist.php returns a value that I need to continue and I can't get the above code to work - UpDist runs, the return value doesn't.


03-05-2012, 07:20 PM
Get rid of the return around the statement adding the script as you are not inside a function and so cannot return anything from there - also that statement wouldn't return anything even if it was in a function.

UpDist.php will then load and you will then be able to run the code inside it just as if it had been attached to a script tag.

03-05-2012, 10:44 PM
OK, you may yell at me - I was too brief. :o

<script type="text/javascript">
function updist()
d = document.createElement("script");
d.type = "text/javascript";
d.src = "/var/www/html/Seis/UpDist.php";
// here I need to return what "d" outputs

var line1 = new TimeSeries();
var line2 = new TimeSeries();
var line3 = new TimeSeries();
setInterval(function() {
line1.append(new Date().getTime(), Math.random()/2);
line2.append(new Date().getTime(), Math.random());
line3.append(new Date().getTime(), updist() );
}, 500);

I'm modifying code from http://smoothiecharts.org/ and I want/need to change "Math.random()" to updist. Right now updist.php does a php echo rand(0,99)/100;. Once I get the JS updist to return the 0.xx to line3 then updist.php will start doing mysql_query select x,y,z from the database.
Unless you have a better way/idea.

I doing another version of http://node.collectivedisorder.com/index.html - He isn't DB'ing the data, I DB the data then display it.


03-05-2012, 10:51 PM
With document.body.appendChild(d) you will load UpDist.php which will obviously output a Javascript source file. As soon as this Javascript source file has been loaded into the browser, it will be executed. That means: All the code belonging to the global scope of this new Javascript will be executed automatically. But: There is no such thing as a "return value" of the global scope.

So, if you want to get something in "return" from executing the global scope of the external script, you'll either have to define global variables inside the external script that you can immediately access from the current script OR you'll have to execute a function from the external script file that returns some value.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum