0

In Pset8 I chose to create a shorturl for the extra feature. I have returned the shorturl from a php script as a string. This should then get displayed for the user in a div. I can console.log the value of the variable shorturl, but when I try to append the url to the div, it is blank. Is this related to "escaping" or a javascript equivalent of htmlspecialchars? Thanks

function showInfo(marker, content, place)
{
    var placeparams = { id : place.postal_code};
    var shorturl = [];

    //$.get("shorten.php", placeparams)

    $.get("shorten.php?id="+place.postal_code)
        .done(function(data) {
            console.log("done");
            shorturl = data;
            console.log(shorturl);

            //shorturl = data;
        })
        .fail(function(errorThrown) {
            // log error to browser's console
            console.log("fail");
            console.log(errorThrown.toString());
        })        
        .always(function(){
            console.log("complete");
            //console.log(shorturl["id"]);
        });


$(document).ready(function(){
    div = "<div id='info'>";
    if (typeof(content) === "undefined")
    {
        // http://www.ajaxload.info/
        div += "<img alt='loading' src='img/ajax-loader.gif'/>";
    }
    else
    {
        div += "<h3>What's up in " + marker.labelContent + "?</h3>" + "<h4>Copy shorturl: " + shorturl.toString() + "</h4>";
        div += content;
    }

    // end div
    div += "</div>"; });
4
  • OK..so I have changed the code up to use a callback which should update the div by id, and cannot figure out how to pass the result to the innerhtml. – Celebrity Butter Churner Oct 6 '15 at 4:20
  • What value do you get in JavaScript Console for shorturl? – Mustaghees Oct 10 '15 at 4:03
  • I have since changed my shorten.php around so that it just returns the url as a string. That console.log(shorturl) displays something like "goo.gl/foo" I suppose I should try to look at the actual value of shorturl via the dev tools. I will look up how to do that in chrome. – Celebrity Butter Churner Oct 14 '15 at 3:39
  • I rewrote most of this and added a button to initiate the shorten. This made me realize that the button is receiving shorturl before the .done has fired off. So I may change it to get the shorturl as soon as the user has opened the info window, then create the button only after .done. – Celebrity Butter Churner Oct 14 '15 at 5:50
0

I was updating the shorturl variable in .done, but the rest of the code had already executed. This is actually a fairly basic error now that I've figured it out. I added $.('#shorturl-div').html(shorturl); inside of .done. Now it waits until it has the shorturl to attempt to update it in the dom.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .