简体   繁体   中英

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.

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/

您必须将<script/>到 html 的<head></head>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM