我正在使用以下代码,它可以完成我希望它做的 3 件事中的 2 件事,通过自定义 Twitter 按钮共享动态内容:
<script type="text/javascript">
// <![CDATA[
var twtTitle = document.title;
var twtUrl = location.href;
var maxLength = 140 - (twtUrl.length + 1);
if (twtTitle.length > maxLength) {
twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
}
var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
document.write('<a href="' + twtLink + '" target="_blank"' + '><img src="images/twitter.png" border="0" alt="Tweet This!" /' + '><' + '/a>');
// ]]>
</script>
我希望它做的也是在窗口中弹出而不是完整的页面 View 。我对脚本的了解有限,所以我不知道在哪里插入适当的弹出代码。
有什么帮助吗?
最佳答案
您需要window.open,而不是document.write。确保该事件是通过单击操作完成的,否则弹出窗口阻止程序将停止您的脚本
<script type="text/javascript">
function fbs_click() {
var twtTitle = document.title;
var twtUrl = location.href;
var maxLength = 140 - (twtUrl.length + 1);
if (twtTitle.length > maxLength) {
twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
}
var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
window.open(twtLink);
}
</script>
并在 HTML 中添加图像标签,如下所示:
<a href="#" onclick="fbs_click();" ><img src="images/twitter.png" border="0" alt="Tweet This!"></a>
希望这有帮助
关于javascript - 具有动态内容和弹出窗口的自定义 Twitter 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831842/