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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Run server side script from javascript

    I'm trying to get a server side script to run from a webpage but I just can't get it to run.
    I've run this:

    Code:
     
    <html>
    <head>
    <script type="text/javascript">
    function runscript()
     {
     alert(“Hello”)
     }
    </script>
    </head>
    <body>
     <li><a href="javascript required.html" onclick="runscript(); return false;" title="Command">RUN</a></li>
    </body>
    </html>
    and it works ok on both Linux and Windows clients. I've tried this but to no avail:

    Code:
     
    <html>
    <head>
    <script type="text/javascript">
    function runscript()
     {
     exec(“/cgi-bin/script.sh”) 
     }
    </script>
    </head>
    <body>
     <li><a href="javascript required.html" onclick="runscript(); return false;" title="Command">RUN</a></li>
    </body>
    </html>
    Here's the script just in case this is the issue but I doubt it:

    Code:
     
    #!/bin/bash
    
    echo "Content-type: text/html"
    echo ""
    
    <some xmlrpc command>
    I'm no coder, this is what I've gleaned from hours of surfing and learning. Any help gratefully accepted.

    Edit: I'm using Apache2 on a Linux Mint 9 LXDE server and the script runs fine using:

    Code:
    <li><a href="/cgi-bin/script.sh" title="Script">RUN</a></li>
    but I have to redirect back to the webpage it's called from and this causes a refresh which is why I'm trying to use javascript.
    Last edited by jonhen; 01-14-2012 at 10:39 AM. Reason: additional information

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    To run something on the server from JavaScript you need to reference a file on the server that contains the code you want to run. This can be an image or another script file or something referenced in an iframe or object but is more usually referenced directly using an XMLHttpRequest call.
    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.

  • Users who have thanked felgall for this post:

    jonhen (01-14-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    xmlhttprequest call

    Hi Stephen thanks for your response and pointing me in the right direction. I had already explored that avenue unsuccessfully but I wasn't sure if that was the correct method or not. Anyway after a little perseverance I have now come up with this:

    Code:
     
    function runscript()
    {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","/cgi-bin/script.sh");
    xmlhttp.send();
    }
    which has solved my problem. Appreciate your help. Thanks.

    Jon
    Last edited by jonhen; 01-14-2012 at 11:59 AM.


  •  

    Posting Permissions

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