# Having a bit of trouble with tweet javascript

**URL:** <https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806>\
**Category:** General\
**Created:** [November 1, 2016, 3:50pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806 "2016-11-01T15:50:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 1, 2016, 3:50pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/1 "2016-11-01T15:50:12Z")

</div>

Hey everyone,

I’ve been playing around with this project for what seems far too long (a couple of days). I think that this is because I’m still piecing together my Javascript.

I have a couple of things I need to solve before moving on:

1. Getting my Twitter call javascript to work. Does anyone have pointers to reference samples? I wasn’t finding useful examples for my current skill level.
2. Want to ensure a consistent height so that the buttons at the bottom don’t jump around so much when I return the quote and author. Any tips on doing this within a row-fluid div?
3. Stil need to add relevant images - was going to do that within the lower right background of the jumbotron box, but not sure how that will display within smaller devices within the responsive framework

[Click through to the project.](http://codepen.io/Velochicdunord/full/VaEaNm/)

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 1, 2016, 5:03pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/2 "2016-11-01T17:03:07Z")

</div>

Adding a Twitter button is quite easy as you can simply create a link with your [URL-encoded](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) quote added to your link’s `href` attribute. Obviously, you need to update this every time you generate a new quote.

The link looks like this:

`https://twitter.com/intent/tweet?text=YOUR+QUOTE+HERE`

It seems you use Bootstrap so a simple button looks like this:

```
<a href="https://twitter.com/intent/tweet?text=YOUR+QUOTE+HERE"
   class="btn btn-info btn-lg"
   target="_blank">Tweet It!</a>

```

You can find more info about it at [Guides | Docs | Twitter Developer Platform](https://dev.twitter.com/web/tweet-button/web-intent)

> [@Velochic](#):
>
> Want to ensure a consistent height

Consistent height of what? You can use the height property…

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 2, 2016, 2:10am UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/3 "2016-11-02T02:10:22Z")

</div>

A bit more frustration here - I see that the version of my page displaying is the earlier version from this morning, not the version I thought I had finished with this afternoon (changes in layout - and a twitter button that wasn’t working correctly)

---

<div class="post-metadata">

**Author:** ![iheartkode](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/iheartkode/32/18_2.png) [@iheartkode](https://forum.freecodecamp.org/u/iheartkode)\
**Post date:** [November 2, 2016, 3:56am UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/4 "2016-11-02T03:56:57Z")

</div>

did you get this resolved? 🙂

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 2, 2016, 8:31pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/5 "2016-11-02T20:31:25Z")

</div>

Redid the work. Still annoyed by the reversion - it was a good thing I was actually doing the work in Sublime and then pasting it back into CodePen.

I got the element height working correctly, but still not getting the Twitter function to run properly. Much reading and looking at other’s code still required.

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 2, 2016, 9:02pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/6 "2016-11-02T21:02:50Z")

</div>

> [@Velochic](#):
>
> I got the element height working correctly, but still not getting the Twitter function to run properly. Much reading and looking at other’s code still required.

Glad you figured out how to set the height. I’ve just checked your code and it seems that you’re still using a `button`, instead of a simple link. Button tags don’t have any `href` attributes.

So, instead of `<button class="btn btn-info btn-lg" ...` it should be:

`<a class="btn btn-info btn-lg" href="https://twitter.com/intent/tweet?text=female-tech-quote+ – +female-tech-name" target="_blank"><i class="fa fa-twitter fa-lg"></i> Share on Twitter!</a>`

Now all you need to do is change the `href` attribute every time you generate a new code. It’s pretty easy too 😉

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 3:29am UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/7 "2016-11-06T03:29:55Z")

</div>

Hrm. I’ve been wrestling with how to get the variable href value to work most of today. Have been reviewing the DOM structure, how to vary stuff… not working. Will trace through the console in the morning.

Am going to share a bit more of the related javascript:

I’m creating the array within my Javascript (start simple).

Then:  
//assign quoteNum varable. Kept it global to access elsewhere in the script

var quoteNum = 0;  
function randomNum(quoteNum) {  
//run function to assign the quote number  
quoteNum = Math.floor(Math.random() \* 10);  
//print num to check result  
console.log(quoteNum);  
return quoteNum;  
}  
//call the functions  
function whenClicked(quoteNum) {  
randomNum();  
//call the values from the array and assign to the elements  
document.getElementById(“female-tech-quote”).innerHTML = quoteArray[quoteNum][0];  
document.getElementById(“female-tech-name”).innerHTML = quoteArray[quoteNum][1];  
document.getElementById(“female-tech-note”).innerHTML = quoteArray[quoteNum][2];  
}

function dynamicURL(quoteNum) {  
//also assign the values to a string for twitter  
var twitterURL = “[https://twitter.com/intent/tweet?text=](https://twitter.com/intent/tweet?text=)”+ quoteArray[quoteNum][0] + " – " = quoteArray[quoteNum][1];  
var result = str.link(twitterURL);  
document.getElementById(“tweettext”) = result;

}

The different parts of the quote, the womens’ name and the associated notes are called by element ID.

That doesn’t transpate when I use the element ID in the twitter button call. I’ve tried creating a string within my Javascript that adds the variable values to the http string, but that isn’t working.

I get “female-tech-quote” - “female-tech-name” as strings within the Twitter tweet message box, but they aren’t showing the array values as they change with each call from the array.

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 6, 2016, 11:51am UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/8 "2016-11-06T11:51:11Z")

</div>

> [@Velochic](#):
>
> var twitterURL = “[https://twitter.com/intent/tweet?text=](https://twitter.com/intent/tweet?text=)”+ quoteArray[quoteNum][0] + " – " = quoteArray[quoteNum][1];

The last equals sign should be a plus sign here.

> [@Velochic](#):
>
> ```
> var result = str.link(twitterURL);
> document.getElementById("tweettext") = result;
> 
> ```

What you’re trying to do with that `str.link()` is not the best option and I don’t think `str` is declared anywhere.

`str.link()` [works](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/link) like this… If you have a string like `'Tweet It'` and another string `'http://twitter.com'`, it creates a new string which can be added with `innerHTML` to your page, making it a clickable link.

```
let twitter = 'Tweet It'
let href = 'http://twitter.com'

let link = twitter.link(href)
/* link is now <a href="http://twitter.com">Tweet It</a> */

```

On the other hand, updating the `href` attribute is as simple as this:

```
document.getElementById('tweettext').href = 'http://example.com'

```

Now, if you click on `#tweettext`, it goes to [example.com](http://example.com). So, it should be easier to simply update the `href` in your `whenClicked()` function 🙂

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [November 6, 2016, 12:11pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/9 "2016-11-06T12:11:17Z")

</div>

> [@Velochic](#):
>
> var twitterURL = “[https://twitter.com/intent/tweet?text=](https://twitter.com/intent/tweet?text=)”

Make sure to use the address `https://www.twitter.com` - that `www` is important. Also, use the function `encodeURIComponent()` on each of the array items you are appending to the URL.

```auto
var twitterURL = "https://twitter.com/intent/tweet?text="+ encodeURIComponent(quoteArray[quoteNum][0]) + " – " + encodeURIComponent (quoteArray[quoteNum][1]);

```

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 1:49pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/10 "2016-11-06T13:49:43Z")

</div>

I had wondered about that. Came across the function in my reading - and tried it out, but couldn’t get it to work while messing around with it yesterday.

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [November 6, 2016, 2:09pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/11 "2016-11-06T14:09:00Z")

</div>

That’s probably because you were missing the `www` from the Twitter link. Without it, Twitter’s server will redirect your request, which re-encodes the URL and will screw up your text parameter†. Of course, this doesn’t stop Twitter from giving developers the exact address you’ve _been_ using in their official documentation, making this a frustrating experience for campers in particular.

_† I’m pretty sure this is what happens, as others have said as much on StackOverflow et al., but it could just as easily be unicorn magic._

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 2:13pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/12 "2016-11-06T14:13:36Z")

</div>

Justed tested the change - the twitter link works with and without the www. The issue I’m having appears to be linked to how the text is being added to the string.

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 2:56pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/13 "2016-11-06T14:56:42Z")

</div>

Am debugging right now. Am using Sublime, not CodePen as the view screens are larger and easier to read on my little laptop. To do that, I’m calling all the scripts from within the header (properly formatted).

Am getting an error when attempting to call [https://platform.twitter.com/widgets.js](https://platform.twitter.com/widgets.js). Is it possible that that is an incorrect source address as well?

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 2:57pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/14 "2016-11-06T14:57:15Z")

</div>

It’s going through to a minified js source.

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [November 6, 2016, 3:09pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/15 "2016-11-06T15:09:20Z")

</div>

That depends - what is the error?

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 3:14pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/16 "2016-11-06T15:14:55Z")

</div>

Two distinct errors, it turns out.

The browser console shows:  
GET file://platform.twitter.com/widgets.js net::ERR\_FILE\_NOT\_FOUND  
The page is looking for the file locally - when the addressing is to the source site.  
This is using Sublime 3.

The second (which was the one I was attempting to trace) is:  
pickQuote.js:72  
Uncaught ReferenceError: documentgetElementById is not defined(…)  
whenClicked @ pickQuote.js:72  
onclick @ quote\_page.html:37

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 3:19pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/17 "2016-11-06T15:19:49Z")

</div>

Let me know if I should post all the code.

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 6, 2016, 6:48pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/18 "2016-11-06T18:48:20Z")

</div>

> [@Velochic](#):
>
> GET file://platform.twitter.com/widgets.js net::ERR\_FILE\_NOT\_FOUNDThe page is looking for the file locally - when the addressing is to the source site.

Are you sure you included the https part?[quote=“Velochic, post:16, topic:50806”]  
Uncaught ReferenceError: documentgetElementById is not defined(…)  
[/quote]

There is a dot missing from `document.getElementById()`.

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 6, 2016, 8:35pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/19 "2016-11-06T20:35:28Z")

</div>

The missing period was the issue. Sharp eye! I had missed that. 🙂

Values transferring properly now.

Now have to figure out how to strip out the HTML I so carefully added to some of the women’s names. Next challenge!

---

<div class="post-metadata">

**Author:** ![Velochic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/velochic/32/13120_2.png) [@Velochic](https://forum.freecodecamp.org/u/Velochic)\
**Post date:** [November 17, 2016, 10:04pm UTC](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806/20 "2016-11-17T22:04:45Z")

</div>

Back again with this project. Took me a while, but I wrote a bit of javascript to evaluate the length of the tweet string, and to trim it by one sentance if it was going to be too long combined with the author’s name.

I built it in JSFiddle, and got it working using console.log() to check results. Twitter link errored out there. [https://jsfiddle.net/Velochicdunord/bm9pney0/](https://jsfiddle.net/Velochicdunord/bm9pney0/)  
Brought it back to my local Sublime setup, and failed to get the twitter window opening properly.

This remains an issue when I brought the code snippet back to my codepen, switching to instruction to open aa new window instead of calling the document DOM. [http://codepen.io/Velochicdunord/pen/NbPrea?editors=1010](http://codepen.io/Velochicdunord/pen/NbPrea?editors=1010)

What am I missing? "Cuz I’m not seeing it.

[Next page](https://forum.freecodecamp.org/t/having-a-bit-of-trouble-with-tweet-javascript/50806.md?page=2)
