0

How can I reload Reddit buttons with jquery? I have load more ajax function and Reddit buttons don't appear when loaded via jquery. I tried to use:

$.ajax({ url: 'http://www.reddit.com/static/button/button2.js', dataType: 'script', 
cache:true});

This is the actual button attached to each post:

<script type="text/javascript">
reddit_url = "http://domain.com/post-num";
</script>
<script type="text/javascript" src="http://www.reddit.com/static/button/button2.js">
</script> 

Thanks

1 Answer 1

0

You can accomplish this by updating the Reddit iframe's "src".

$('some-surrounding-div-here').find('iframe').attr('src','http://www.reddit.com/static/button/button2.html?width=51&url=http%3A%2F%2Fdomain.com%2Fpost-num');

Here it is in a full working example:

<html>
<head>    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript"> 
         $(function(){
             $('#refresh_reddit_button').click(function(){
                   $('#reddit_button_container').find('iframe').attr('src','http://www.reddit.com/static/button/button2.html?width=51&url=http%3A%2F%2Fdomain.com%2Fpost-num');
             });
         });
    </script>            
</head>

<body>
     <input id="refresh_reddit_button" type="button" value="Refresh Reddit">
     <div id="reddit_button_container">        
           <script type="text/javascript">
                  reddit_url = "http://domain.com/post-num";
           </script>
           <script type="text/javascript" src="http://www.reddit.com/static/button/button2.js">
           </script> 
     </div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Did you try the whole code sample? When you click the "Refresh Reddit" you can see the reddit button below refreshes (for example, I actually see the text 'submit' animate again). However, maybe you need it to do something more, like bring in different reddit content or something??
although it selects and changes the link of the reddit button, it doesn't work for buttons loaded by POST method
i discovered that when loading the buttons through POST, iframe of the button will not be created so changing the SRC won't work because the buttons isn't there at all

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.