Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    document.body.appendChild return value

    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.

    Thanks
    Chuck

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,616
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, you may yell at me - I was too brief.
    ------------------------------

    <script type="text/javascript">
    function updist()
    {
    d = document.createElement("script");
    d.type = "text/javascript";
    d.src = "/var/www/html/Seis/UpDist.php";
    document.body.appendChild(d);
    // 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);

    ...
    </script>
    -----------------------------------
    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.

    Thanks

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •