Pancake_Senpai
Pancake_Senpai

Reputation: 945

How to input javascript variables in to html code?

I am trying to create a mileage calculator for an app, and I found the code online.

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var directionDisplay;
var map;


function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
    zoom:12,
   `enter code here` mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: copenhagen
}

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}


var directionsService = new google.maps.DirectionsService();

function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");

var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
    }
});
}
</script>

<title>Distance Calculator</title>

<style type="text/css">

        body {
            font-family:Helvetica, Arial;
        }
        #map_canvas {
            height: 50%;
        }
</style>
</head>
<body>

<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
    <div>
        <p>
            <label for="start">Start: </label>
            <input type="text" name="start" id="start" />

            <label for="end">End: </label>
            <input type="text" name="end" id="end" />

            <input type="submit" value="Calculate Route" onclick="calcRoute()" />
        </p>
        <p>
            <label for="distance">Distance (km): </label>
            <input type="text" name="distance" id="distance" readonly="true" />
        </p>
    </div>
    <div id="map_canvas"></div>
</body>
</html>

javascript google geolocation maps 

I want there to be a click to email link on the link on the page which opens up users email (such as Windows Live Mail on a PC or the Yahoo Mail app on a tablet) and says in the body of the email "Your mileage is ???". I can do this, but I want the mileage to be inputted automatically. I have added in this code in the html section of the script...

<p><a href="mailto:youremail.yahoo.com?subject=Mileage
&body=calcRoute('string', 'distance')">Click to send email</a></p>

...and it opens the email but doesn't show the calculated mileage. Instead in the body it shows calcRoute('string, distance'). How do I get it to show the mileage?

I understand this question is long but I would appreciate it if you could take the time to answer. It is very important that I get an answer that works as soon as possible. Thank you.

Upvotes: 0

Views: 388

Answers (2)

user2585506
user2585506

Reputation: 71

You have to compute the mailto attribute value in the link onclick event. Supposing calcRoute function returns a string, the following code should work:

<a href="mailto:youremail.yahoo.com?subject=Mileage&body="
   onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a>

Upvotes: 1

Daniel
Daniel

Reputation: 598

You should check out this link should be helpful

My guess is that it's not detecting the subject. Are you sure your javascript CalcRoute is returning a variable?

Upvotes: 0

Related Questions