简体   繁体   中英

Tweet Button Javascript

I'm trying to make a shortcut function so that if somebody presses Alt+T they can tweet the article. I have the shortcut part sorted so ignore that part, it's just tweeting the title and URL of the page. 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. 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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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