Skip to content

Instantly share code, notes, and snippets.

@McKinneyDigital
Created June 6, 2012 20:21
Show Gist options
  • Select an option

  • Save McKinneyDigital/2884508 to your computer and use it in GitHub Desktop.

Select an option

Save McKinneyDigital/2884508 to your computer and use it in GitHub Desktop.
Share to Twitter from a link using Javascript
// Opens a pop-up with twitter sharing dialog
var url = "http://google.com";
var text = "Replace this with your text";
window.open('http://twitter.com/share?url='+encodeURIComponent(url)+'&text='+encodeURIComponent(text), '', 'left=0,top=0,width=550,height=450,personalbar=0,toolbar=0,scrollbars=0,resizable=0');
@rileyrichter

Copy link
Copy Markdown

This is great! Thanks for sharing! One question....what do you do when you have commas, quotes, ampersands, etc? They get converted as well and it provides a challenge. Any workaround for that?
screenshot 2017-11-14 15 01 20

@alvarezgarcia

Copy link
Copy Markdown

Simple and nice.

Thanks!

@EngrMikolo

Copy link
Copy Markdown

Thanks.
Very straightforward

@tassiocaique

Copy link
Copy Markdown

Just a little improvement to easily add other params:

      // Opens a pop-up with twitter sharing dialog
      var shareURL = "http://twitter.com/share?"; //url base
      //params
      var params = {
        url: "http://google.com", 
        text: "Replace this with your text",
        via: "sometwitterusername",
        hashtags: "hashtag1,hashtag2"
      }
      for(var prop in params) shareURL += '&' + prop + '=' + encodeURIComponent(params[prop]);
      window.open(shareURL, '', 'left=0,top=0,width=550,height=450,personalbar=0,toolbar=0,scrollbars=0,resizable=0');

@jasson-33

jasson-33 commented Jun 22, 2018

Copy link
Copy Markdown

Thanks ! working like a charm !

@JuanBalceda

Copy link
Copy Markdown

Thanks a lot!

@ankita-cears

ankita-cears commented Apr 11, 2019

Copy link
Copy Markdown

Hello,
I want to share an image for my site's promotion on twitter so what I have to pass in below code

`var url = "http://google.com";
var text = "Replace this with your text";

window.open('http://twitter.com/share?url='+encodeURIComponent(url)+'&text='+encodeURIComponent(text), '', 'left=0,top=0,width=550,height=450,personalbar=0,toolbar=0,scrollbars=0,resizable=0');
`

@AliKazim7

Copy link
Copy Markdown

Can we share image in this view?

@true-eye

Copy link
Copy Markdown

How can we share image? Any solution, please?

@Bibsiroglu

Copy link
Copy Markdown

How can we share image?

@marcusx2

Copy link
Copy Markdown

@tassiocaique How can we share image??

@jessecaii

Copy link
Copy Markdown

How can we close the window after posting the link?

@zhengyimeng

Copy link
Copy Markdown

How can we share image?

@tylerjusfly

tylerjusfly commented Nov 29, 2024

Copy link
Copy Markdown

You can only share image through metadata i guess, that's the only way i have been able to figure out.

for example in NextJs

const article = {
  title: "Hello me",
  description: "this is a test for descripton",
  imageUrl:
    "https://media.npr.org/assets/img/2018/04/20/gettyimages-495019617_wide-70695696742f3348b675ebf762df2f3b8256f1dd.jpg?s=1100&c=85&f=jpeg",
  slug: "test-me",
  content: "HEY BROOOOO",
};

export async function generateMetadata() {
  // Assuming `params.slug` matches the article's slug
  return {
    title: article.title,
    description: article.description,
    twitter: {
      card: "summary_large_image",
      title: article.title,
      description: article.description,
      images: [article.imageUrl],
    },
  };
}

... Rest of the component

@tylerjusfly

Copy link
Copy Markdown

If anyone finds a way to share , without metadata, I'd be happy to hear it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment