On click event of this button we want to post a tweet. For that we need to navigate to user to URL
After status we can put text to be tweeted. So on click event we need to navigate to this URL and append text to be tweeted. Very first we need to check whether tweet length is less than 140 or not. If it is more than 140 then we will show error message to user.
txttoTweet is variable containing text to be tweeted. In Else part we can tweet by redirecting to said URL as following,
Full code is as following,
Some of the important point worth discussing here is as following,
- Text to Tweet must be inside encodeURIComponet . Else you will get error.
- Check Text to Tweet is less than 140 characters.
- If you want can open windows as dialog also.
On running above script you should be getting output as following,
On click of Tweet button in next tab or windows as of your browser setting you will be asked to log in to Twitter, if not already logged in
On successfully sign in you should be able to tweet.