0

I need to implement a calculator on my webpage, which will receive some arguments in the url and is supposed to return just the result, nothing else.

Example:

http://example.com/calc/?x=244&y=123&op=plus

I wrote a program using HTML and JavaScript which returns the correct result and prints it on the webpage.

This prints the result. But the client doesn't just receive the result, but the whole html & script. Does anyone know how I can send only the result to the client?

Edit: If this cannot be done with HTMP and JS, how else?

<!DOCTYPE html>
<html>

<body>
  <p id="demo"></p>
  <script>
    function calculate(url) {
    //  ...
    }

    let url = window.location.href;

    document.write(parseInt(calculate(url)));
  </script>
</body>

</html>

Desired result: 367 is returned to the HTTP client

Actual result: < !DOCTYPE html>

.... ...... is returned to the HTTP client
Dreana
  • 573
  • 4
  • 16

1 Answers1

-1

Try the following

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>

function calculate (url_string) {
    try {
        var url = new URL(url_string);
        var x = parseInt(url.searchParams.get("x"));
        var y = parseInt(url.searchParams.get("y"));
        switch(url.searchParams.get("op")){
            case "plus": return x + y;
            // add more cases here
        }
    } catch(e){}
}

let url = window.location.href;

document.write(parseInt(calculate(url)));
</script>
</body>
</html>
Rajind Pamoda
  • 143
  • 2
  • 10
  • _I wrote a program using HTML and JavaScript which returns the correct result and prints it on the webpage._ so obviously a droplet does not work that way – mplungjan Jan 12 '21 at 12:55