Embed <script> through jQuery?

I am trying to add a twitter script along with some HTML via a for each loop. The loop works fine until I add tags <script>at the end. I am trying to make jQuery spit out my data and then put the twitter button below the information.

I am wondering if there is another way to get jQuery to add a script to my div #entries.

My code is below:

$.each(data.Entries, function(entriesIndex, entriesObject) {

    if (entriesObject.Field21 == "Yes") {

        if (entriesObject.Field15 == "Yes") {

            $("<li />", {
                html: '<p><a href="http://twitter.com/share">foo</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></p>'


                // Add it to the page
            }).appendTo("#entries");

        } // end if entry = yes
    } // end if approved = yes

}); // end for each loop

Update

It seems like it puts the script on the page, but it doesn’t work - it’s not very familiar with how the twitter script works, but I wonder if you guys could help! Thank!

See an example page here: http://bostonwebsitemakeover.com/vote.html

    $.each(data.Entries, function(entriesIndex, entriesObject) {

        var p, tweet; // oh hey look, variables!

        if (entriesObject.Field21 == "Yes") {

            if (entriesObject.Field15 == "Yes") {

                     p = $("<p />", {
                    html: "<p>" + '<a href="' + entriesObject.Field19 + '" target="_blank">' + entriesObject.Field4  +  "</p>" + '<img src="http://images.websnapr.com/?url=' + entriesObject.Field19 + '" />' + "</a>"
                    });

                     tweet = $('<script>').attr({
                        type: 'text/javascript',
                        src: 'http://platform.twitter.com/widgets.js'

                    });

            } // end if entry = yes

        } // end if approved = yes


        $('<li class="entry">').append(p).append(tweet).appendTo('#entries');

}); // end for each loop
+3
source share
4 answers
$('<script>').attr({
  type: 'text/javascript',
  src: 'http://platform.twitter.com/widgets.js'
}).appendTo('#entries');

, . , .attr,.text .. jQuery . XSS.

, , , . , jsfiddle, . , ... Nevermind, , , DOM Google chrome/firebug ( ).

+2

, . . script, , , twitter.com -. .


script , widget.js script? , script, ? , live(), 'click' , . , ​​ .

+1

; onload, - script script.

, , .

0

, <a> "twitter-share-button"

, , script ( iframe)

, script , , javascript

Edit http://dev.twitter.com/pages/tweet_button_faq#multiple-buttons definitely loads the script only once

0
source

Source: https://habr.com/ru/post/1787556/


All Articles