Search code examples
javascriptwidgettwitterinnerhtml

Twitter Widget Not Working In My JavaScript Function


I built a Twitter Widget and I am trying to get it to display on my page inside a prebuilt JavaScript function.

I am putting the widget inside a hidden div that displays when a link on the page is clicked, but the widget isn't displaying and I don't know why. I'm copying the code from Twitter exactly.

Here is the JavaScript function and Twitter Widget code:

function showTwitterFeed(a) {
    $(".navlink").removeClass("active");
    $(a).addClass("active");
    $("#content").css("display","block");
    document.getElementById('content').innerHTML = "<a class=\"twitter-timeline\" width=\"500\" height=\"600\" href=\"https://twitter.com/PLInVancity\" data-widget-id=\"293993973104656384\">Tweets by @PLInVancity</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
}

If anyone knows why it's not working please let me know. It would be appreciated.


Solution

  • function showTwitterFeed(a) {
        $(".navlink").removeClass("active");
        $(a).addClass("active");
        $("#content").css({"display": "block"});
        $('#content').html("<a class=\"twitter-timeline\" width=\"500\" height=\"600\" href=\"https:\/\/twitter.com\/PLInVancity\" data-widget-id=\"293993973104656384\">Tweets by @PLInVancity<\/a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"\/\/platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");<\/script>");
    }
    

    That fixed it, I believe (not receiving any more errors in JsFiddle). You needed to escape all of the "/".

    EDIT: Definitely fixed, here's the fiddle: http://jsfiddle.net/klatzkaj/73A3m/