Custom tweet button with javascript

I was following the topic Build Your Own Tweet Button on https://dev.twitter.com/docs/tweet-button

They explicitly say

JavaScript Interfaces for Twitter are not available for custom Tweet Buttons.

But we can have a custom tweet button (or link) and still have the Twitter javascript  API (web intents).

All you have to do is to include the javascript file for the Twitter API:


<script src="https://platform.twitter.com/widgets.js"></script>

Your custom button (link in this example):


<a href="https://twitter.com/intent/tweet">Tweet</a>

This is the trick, instead of the link pointing to https://twitter.com/share

your link should point to https://twitter.com/intent/tweet

This way you still be able to use Twitter API. The following code shows how you can get

the tweet event:


twttr.events.bind('tweet', function (event) {
  // Do something there
  alert('Tweeted');
});

Check the online demo

Advertisements

jQuery to prevent repeat myself for a long validation form

It’s quite frustrating when we spend a some time doing a jsFiddle and preparing an answer for Stack Overflow question and see the answer being removed by its user!

I’ve decided at least post it here, so that it wouldn’t be a total waste of my time.

The question was about how to reuse javascript code to perform validation of the user input.

Demo on jsFiddle

HTML code:


<input type="text" id="txtEmail"/>

<input type="button" id="btnSubmit" value="Submit" />

Javascript code:

var myValidations = function () {

  var isValidEmail = function(email){
    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)){
      return false;
    }
    else{
      return true;
    }
  };

  return {
    isValidEmail: isValidEmail
  }
}();

$(function(){

  $('#btnSubmit').on('click', function() {
    if(myValidations.isValidEmail($('input#txtEmail').val())){
      alert('Email is valid!');
    }
    else{
      alert('Email is not valid!');
    }
  })

})

The code above uses the Revealing Module Pattern to create a namespace for validations.
On myValidations we can add other functions to validate any user input.
In this example I have an email validation function that I can call from anywhere by passing the user input.