最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Post tweet with new line inside it - Stack Overflow

programmeradmin3浏览0评论

I know that twitter says that users should not use new lines inside tweets because it depends on the client if it are shown or not, but im coding an app that draws tweets into the twitter timeline aLa . So its a must.

The thing is that after i tried all the possible ways of doing this i cannot force the tweet to be split in multiple lines. I had revised how they post it, and sending the exact same "newline" code it doest not show as drawatweet does.

I have tried using the twitter api and the twitter intent tweet page without success.

Another example its / that use multiple lines for showing a message, and, again, i use the same mechanism but my new lines dont work.

I came to think that Twitter has some arrange with this "apps" and that its being check by the referrer. I know that this sounds crazy, but i cannot find another logical answer. Does somebody knows how it work?

UPDATE

There its no possible way to use new lines in twitter. What i have todo is to generate really large strings (without spaces inside) and the new line (or space), will force a new line if both strings are unsplittable.

Thanks in advance gonzalo

I know that twitter says that users should not use new lines inside tweets because it depends on the client if it are shown or not, but im coding an app that draws tweets into the twitter timeline aLa http://www.drawatweet.com . So its a must.

The thing is that after i tried all the possible ways of doing this i cannot force the tweet to be split in multiple lines. I had revised how they post it, and sending the exact same "newline" code it doest not show as drawatweet.com does.

I have tried using the twitter api and the twitter intent tweet page without success.

Another example its http://www.preservatweets.com/ that use multiple lines for showing a message, and, again, i use the same mechanism but my new lines dont work.

I came to think that Twitter has some arrange with this "apps" and that its being check by the referrer. I know that this sounds crazy, but i cannot find another logical answer. Does somebody knows how it work?

UPDATE

There its no possible way to use new lines in twitter. What i have todo is to generate really large strings (without spaces inside) and the new line (or space), will force a new line if both strings are unsplittable.

Thanks in advance gonzalo

Share Improve this question edited Feb 28, 2013 at 19:36 Gonzalo asked Feb 27, 2013 at 20:58 GonzaloGonzalo 731 gold badge1 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

Easiest way to do this is..

var msg = "This is a test message. \n is used for new line character.";
var twitterHref = "http://twitter.com/intent/tweet?text=" + escape(msg);

and then set it dynamically as href for your <a/>.

Twitter's website never displays the new lines in tweets, it just seems that way. Some clients do display the new lines, but Twitter does not. They simply display them as they would a space. They do leave the actual new line character in the tweet though.

The reason it seems like drawatweet is working is because of word wrap. It doesn't matter what characters or character set you use and it doesn't matter how many characters you use. If the next word in the tweet is too long to fit in the width of their paragraph, it will wrap. They have made "words" (string of characters with not spaces) out of block characters, but you can use any character you want.

To test this, replace their new lines with spaces. It still wraps. You can also add a space to the second line and you'll see the new line is not preserved, as the first part of the second line will now be on the first line. Replace the blocks with your own characters and it will work, too.

So the solution is to fake it, like they do, and make lines long enough to always wrap. If you're dealing with single "words", you can use the unicode no-break space (%C2%A0) to replace spaces where you don't want it to wrap. You can also pad short lines with this to make them long enough to wrap.

Demo:

<a href="https://twitter.com/intent/tweet?text=123456789012345678901234567890%0A123456789012345678901234567890%0A123456789012345678901234567890" target="_blank">long lines with \n - works!</a><br />
<a href="https://twitter.com/intent/tweet?text=123456789012345678901234567890%20123456789012345678901234567890%20123456789012345678901234567890" target="_blank">long lines with spaces instead of \n - works! (same as \n)</a><br />
<a href="https://twitter.com/intent/tweet?text=123456789012345678901234567890%0A12345678 9012345678901234567890%0A123456789012345678901234567890" target="_blank">long lines with \n - space in line - doesn't work</a><br />
<a href="https://twitter.com/intent/tweet?text=123456789012345678901234567890%0A12345678%C2%A09012345678901234567890 %0A123456789012345678901234567890" target="_blank">long lines with \n - no-break space (%C2%A0) in line - works!</a><br />
<a href="https://twitter.com/intent/tweet?text=12345678901234567890%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%0A12345678901234567890%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%0A12345678901234567890" target="_blank">short lines with \n - no-break space (%C2%A0) padding - works!</a><br />
发布评论

评论列表(0)

  1. 暂无评论