Search code examples
javascriptc#google-mapsurihref

JavaScript not treating value as string when redirecting web-page


I have a selection of buttons which when pressed navigate to a URL, this web page is actually embedded in a program and that navigated to URL acts as a parameter for the program to use, as such the URL is actually a selection of different strings. For example:

'030056310001' '039026320003' '340003253OPP'

Some of the parameters have alphabetical characters, some our just numbers and some only have numbers less than 7 so are treated like octal numbers

How would I treat all the values as strings such that they navigate to the specific URI?

 var content =  '<button type="button" onclick="location.href=' + actual_JSON[key].location_code + '">Learn More</button>';

The code above shows when the buttons are detailed, bellow is the full code any help would be appreciated.

    for (var key in actual_JSON) {

        var infowindow = new google.maps.InfoWindow();
        var content = '<h3> Name: ' + actual_JSON[key].description + '</h3> <p> ID: ' + actual_JSON[key].location_code + '</p><p> Bearing: ' + actual_JSON[key].bearing + '<p> <button type="button" onclick="location.href=' + actual_JSON[key].location_code + '">Learn More</button>';
        console.log(actual_JSON[key].location_code);
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(actual_JSON[key].latitude, actual_JSON[key].longitude),
            map: maps,
            title: actual_JSON[key].description,
        });


        google.maps.event.addListener(marker, 'click', (function (marker, content, infowindow) {
            return function () {
                infowindow.setContent(content);
                infowindow.open(map, marker);
            };
        })(marker, content, infowindow));
        markers.push(marker);
    }
}

Solution

  • As suggested by Satpal -

    Enclose location_code in quotes i.e. onclick="location.href=\'' + actual_JSON[key].location_code + '\'"

    I'll leave the question up for any else who might need it.