Dynamically changing text in twitter button (rails+jquery)
Ok, so i have index.html.erb
. In it I have menu, which I render from _menu.html.erb
, and div, in which I render the link from show.html.erb
by using show action in my controller. Link is shown by <%=link_to h(@link), h(@link) %>
(code from show.html.erb).
Div, in which link is rendered, is often refreshed by $('#div_with_link').load('controller/show');
, so @link changes a lot.
The problem is, that I don't know, what should I put in my twitter button text area. I want people to be able to share the current link. I've tried data-text="<%=@video %>"
, but than it just showing the link on my webpage, not generated one.
What should I do, to put the generated link in my twitter button? An开发者_如何学编程y suggestions would help, thank you in advance.
Alright, if you're using twitter's default button this will be pretty hard to achieve. I'd recommend to simply build your own button and update the href
attribute for this link via Javascript including the mentioned parameters. Here's some code to get you started:
Use this instead of your Twitter button and style it accordingly:
<a href="http://twitter.com/share" id="tweet">Tweet</a>
Every time you load something new into the div, set the new URL (and other params if you want to) e.g. like this:
// not knowing your app, I'm just guessing here:
var twitterParams = {
url: <%= url_for(@video) %>,
text: <%= @video.title %>
};
$('#tweet').attr('href', "http://twitter.com/share?" + $.param(twitterParams));
You could do that e.g. in the piece of HTML that you're returning when you're loading something new via AJAX.
精彩评论