Home>

I want to tweet by retrieving the text written in HTML"texterea>"in JavaScript

I am a beginner who recently started studying JavaScript.
I would like to create a text area on the site and create a function to tweet the contents for various reasons
After pressing the Tweet button, a new window is displayed, but the following error message is displayed.

Error message
Browser: Google Chrome
This page is not working
If this problem occurs repeatedly, contact the site owner.
HTTP ERROR 400
Applicable source code
<form name = "result_form">
<textarea name = "result" rows = "5" cols = "60"></textarea>
<p><input type = "button" value = "tweet text" onclick = "twitText ()" /></p>
function twitText () {
  var s, url;
  s = document.result_form.result.value;
  url = document.location.href;
  if (s! = "") {
    if (s.length>140) {
      //Character limit
      alert ("Text exceeds 140 characters");
    } else {
      // Open the posting screen
      url = "http://twitter.com/share?url=" + escape (url) + "&text = '" + s + "'";
      window.open (url, "_ blank", "width = 600, height = 300");
    }
  }
}


It works when the variable [s] on the 3rd line of JavaScript is set to any text, but it does not work when the above description is used.
Could you tell me how to improve? Also, if you need TwitterAPI to improve it, I would appreciate it if you could supplement it.

  • Answer # 1

    I tried to adjust only where the writing was a little

    function twitText () {
      var s, url;
      s = document.querySelector ('[name = "result_form"] [name = "result"]'). value;
      url = document.location.href;
      if (s! = "") {
        if (s.length>140) {
          alert ("Text exceeds 140 characters");
        } else {
          url = "http://twitter.com/share?url=" + encodeURIComponent (url) + "&text = '" + encodeURIComponent (s) + "'";
          window.open (url, "_ blank", "width = 600, height = 300");
        }
      }
    }

  • Answer # 2

    I tried local file execution (file: ///) and Web server execution (http), but I could not confirm the phenomenon. .
    I'm trying Google Chrome latest and FireFox latest.

    Please check the following so that you can try some.

    https

    url = "https://twitter.com/share? // Here omitted

    Excluding single quotes enclosed in the part where the acquisition text is set

    url = "https://twitter.com/share?url=" + escape (url) + "&text =" + s;

    400 error is an error indicating that the sent request is invalid (or malformed).

    Error message: HTTP error 400

    Maybe there is something wrong with how to make the request.
    The message you are trying to send contains an incorrect wording, or
    The character limit has been exceeded.
    In any case, Twitter has a character limit of 140 characters, and if the URL to be included in the tweet is also included, the number of characters of comments that can be entered will be even shorter.
    Please check from that point of view. Try not to pass the url or just send a short comment.