SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically Inserting <script> tags into HTML on Page Load

    I'm trying to dynamically insert the Tweetmeme button using Javascript. I'm currently using jQuery throughout the site. Here's the script that I'm using. I basically want to cycle through all the blog entries with a class of journal-entry and append the following JavaScript to the end. This Javascript comes straight from tweetmeme.com. This doesn't work for me though and it has something to do with the code between append(). It doesn't like the second set of script tags.

    Code:
    <script type="text/javascript"> 
    $(document).ready(function() {
    
    $('.journal-entry').each(function(index) {
        $(this).append('<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>');
       });
    
    });
    </script>
    Any help is appreciated. Thanks.

  2. #2
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to escape the backslash in the script closing tag:
    Mauricio Samy Silva
    http://www.maujor.com/

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    use getScript... http://api.jquery.com/jQuery.getScript/
    Also you should only load a script once...ie. remove it from the each iteration.

    Code JavaScript:
     
    $.getScript( "tweetme.js",
      function () {
        $( ".some-selector" )
          .each(
            function () {
              // Do what ever tweetme does...
            }
          );
      }
    );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •