ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using EE Variables within Javaccript Code

December 02, 2010 2:00pm

Subscribe [3]
  • #1 / Dec 02, 2010 2:00pm

    AdamBaney

    65 posts

    I’ve inserted a Google Map into a page, but it won’t work without the Latitude and Longitude entered. To do this, the Lat. and Lon. are inserted into Javascript code, as shown below. This code is not a complete code, but just the problem area. Is there a way to insert EE variables into javascript like this? Thanks!

    < script type="text/javascript" >
    function initialize() {
    var myLatlng = new google.maps.LatLng({ee_latitude},{ee_longitude});
    < /script >
  • #2 / Dec 03, 2010 9:26am

    Ingmar

    29245 posts

    I am not suer that will work, due to some parse order. That said, have you tried it? What happens when you hardcode the values? Moving to the CodeShare Corner.

  • #3 / Dec 03, 2010 12:21pm

    AdamBaney

    65 posts

    Trying to insert the EE variables in the JS shows a blank box where the Google Map should be. Hard-coding the latitude and longitude numbers shows the correct map.

    I tried using ASCII code for the brackets ( &# 123; for { and &# 125; for } ), but that didn’t work. I also used double and single quotes, but that didn’t work either. In both cases, the Google Map was blank. This may not be possible to insert EE variables into JS like this. Do you have any other ideas? Thanks for your help!

  • #4 / Dec 03, 2010 1:21pm

    James Smith

    259 posts

    I’ve fought against this one a few times and it can be tricky - what works in some contexts doesn’t always work in others - usually depends whether you’re trying to input a string or a number.

    The easiest way for strings might be something like this:

    < script type="text/javascript" >
    var theLat = "{ee_latitude}";
    var theLong = "{ee_longitude}";
    function initialize() {
    var myLatlng = new google.maps.LatLng(theLat,theLong);
    < /script >

    However, that might not work for you since the map object is probably expecting a proper number - you can convert it from a string to number using parseFloat:

    < script type="text/javascript" >
    var theLat = parseFloat("{ee_latitude}");
    var theLong = parseFloat("{ee_longitude}");
    function initialize() {
    var myLatlng = new google.maps.LatLng(theLat,theLong);
    < /script >

    Try that…

  • #5 / Dec 03, 2010 2:10pm

    AdamBaney

    65 posts

    Thanks, At the Gates!  Actually, both JS versions worked! You’re a genius! I couldn’t program JS to save my life. 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases