简体   繁体   English

推文按钮Javascript

[英]Tweet Button Javascript

I'm trying to make a shortcut function so that if somebody presses Alt+T they can tweet the article. 我正在尝试创建一个快捷方式功能,以便如果有人按下Alt + T,他们可以推文。 I have the shortcut part sorted so ignore that part, it's just tweeting the title and URL of the page. 我将快捷方式部分排序,因此忽略该部分,它只是发布页面的标题和URL。 Here's what I have so far 这是我到目前为止所拥有的

<script type="text/javascript">
shortcut.add("Alt+T",function() {
window.open("https://twitter.com/intent/tweet?text=(document.title) - &url=(document.URL)&via=jamiebrittain")
});
</script>

It seems that document.title and document.url aren't changing to page title and URL. 似乎document.titledocument.url没有更改为页面标题和URL。 This is all in a script tag stating it's javascript so why is not changing or does document.title and document.url not work? 这是一个脚本标签,说明它是javascript所以为什么不改变或document.titledocument.url不起作用?

Instead of 代替

window.open("https://twitter.com/intent/tweet?text=(document.title) - &url=(document.URL)&via=jamiebrittain")

do

window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(document.title)+"&url="+ encodeURIComponent(document.URL)+"&via=jamiebrittain")

you need to concatenate the string with the javascript variables instead of using them inline. 你需要将字符串与javascript变量连接起来,而不是内联使用它们。

Thanks @Esailija for pointing out that you need to use encodeURIComponent too - this ensures that the variables are encoded before being added to the string 感谢@Esailija指出你也需要使用encodeURIComponent - 这确保变量在被添加到字符串之前被编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM