Content Copy Protection by Using Simple Javascript

I just want to share content copy protection by using a few lines of javascript. You can freely modify the script based on your needs.

The first thing you must not forget is adding jQuery inside <head> tag. Enough, here is the script.

/* I use jQuery 1.7.1 */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type = "text/javascript">
(function ($) {
  if ($.browser.mozilla) {
    $.fn.disableTextSelect = function () {
      return this.each(function () {
        $(this).css({
          'MozUserSelect': 'none'
        })
      })
    };
    $.fn.enableTextSelect = function () {
      return this.each(function () {
        $(this).css({
          'MozUserSelect': ''
        })
      })
    }
  } else if ($.browser.msie) {
    $.fn.disableTextSelect = function () {
      return this.each(function () {
        $(this).bind('selectstart.disableTextSelect', function () {
          return false
        })
      })
    };
    $.fn.enableTextSelect = function () {
      return this.each(function () {
        $(this).unbind('selectstart.disableTextSelect')
      })
    }
  } else {
    $.fn.disableTextSelect = function () {
      return this.each(function () {
        $(this).bind('mousedown.disableTextSelect', function () {
          return false
        })
      })
    };
    $.fn.enableTextSelect = function () {
      return this.each(function () {
        $(this).unbind('mousedown.disableTextSelect')
      })
    }
  }
})(jQuery);
jQuery(function ($) {
  $('YOUR_CLASS_OR_ID_HERE').disableTextSelect()
});
</script>

Setting Up

At line 49 you can see YOUR_CLASS_OR_ID_HERE, you have to change it to your content container ID or class. It is to determine which content that you want to copy protect.

Remember to add # before ID name, and . before class name. For example:

/* ID */
$('#the-content').disableTextSelect();

/* CLASS */
$('.the-content').disableTextSelect();

That’s it, Hope it helps. Any question just ask.

Make Facebook Like Button Calls Analytic Function or Action

Recently, I got mad and dizzy because my Facebook Like button wasn’t returning the action I was hoping for, but now I’ve make my dream come true. To make your Facebook Like returning something for example a function or an action you must put the right asynchronous Javascript code from Facebook.

Before you get involved in this tutorial it is necessary to fulfill the following requirements:

First step: Adding xmlns:fb as <html> attribute

Modify your doctype to be HTML5 and <html> tag at very top of your page until it looks like line below.


<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">

Second Step: Adding Facebook Asynchronous Javascript

Facebook Application Developer screenshot
Go to Facebooks Developer page to get your App ID

Add this Facebook Asynchronous script after <body> tag. Please change YOUR_APP_ID_HERE to your Facebook App ID before you save your work.


<div id="fb-root"></div>
<script type="text/javascript">
<!--
window.fbAsyncInit = function() {
    FB.init({appId: 'YOUR_APP_ID_HERE', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('edge.create', function(targetUrl) {
        try{
             _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
             alert('Thank you for your like!');
        } catch(err) {}
    });
    FB.Event.unsubscribe('edge.remove', function(targetUrl) {
        try{
             _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
        } catch(err){}
    });
};

(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol + '//connect.facebook.net/id_ID/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());
//-->
</script>

Google Analytics tracking

On 8th line you see _gaq.push([‘_trackSocial’, ‘facebook’, ‘like’, targetUrl]); That’s Google Analytics asynchronous tracker that will be fired when Facebook Like clicked. It will send clicked data to your Google Analytics.

Alerting when Liked

On 9th line you see alert(‘Thank you for your like!’); That’s what your visitors will see if they click your Facebook Like. Alerting visitors who clicked Like on your website page is good respect, isn’t it? You can change the alert as you like.

Fire your own functions

You can also add your own Javascript function to fire in the script. You may also add script from another Google service for example Google Website Optimizer script. Just remove or add at respective lines above.

Third Step: Adding Facebook Like button

Add like button anywhere you like, but keep it rational. Don’t forget to chage URL_HERE with your desired URL.


<fb:like href="URL_HERE" layout="standard" send="no" layout="standard" font="arial" show-faces="false" width="300" action="like" colorscheme="light"></fb:like>

If you have any questions I will be gladly to answer it 🙂

PHP Example: preg_match() USA Telephone Number Matching

I’ve practiced about how to match USA telephone number format in different format using preg_match() function. The first time I found the exercise is in the Learning PHP 5 by David Sklar. In B8 exercise we have to write PHP script: regular expression that flexibly matches a U.S. phone number whether or not it has parentheses around the area code and has its parts separated by spaces, hyphens, or periods.

Of course, there is the answer for this exercise. But, when I tried, It didn’t smoothly solve the problem. The script wasn’t able to filter double parentheses like “((“, “))”, “(( )”, or “( ))”. So, I have a will to fix it and here the fixed script.

Continue reading PHP Example: preg_match() USA Telephone Number Matching

How to Put Date from MySQL to Select HTML Tag with PHP

You may want to create editable data using PHP and HTML. You want to take your date type value to your <select> HTML tag. It is not difficult, but yes we need to be patient to code it.

First, you should call a query to your MySQL such as:

<?php
$sqlquery=mysql_query("select * from income where transaction_id='".$_GET['transaction']."'") or die(mysql_error());
$row = mysql_fetch_array($sqlquery);
?>

Second, you have to set the type of your date in your table to DATE type. Don’t set it to VARCHAR or INTEGER. Let your MySQL database rules what it’s programmed to be. You just need to explode, separate, and put it into different <select> tag, as follow:

Continue reading How to Put Date from MySQL to Select HTML Tag with PHP

How to Store Date from Select HTML Tag to MySQL

Storing a date from <select> HTML tag to MySQL database is not that easy. I spent three hours to find out how. First time I tried to, I was thinking that my script was not effective. But, after reading some tutorial I found my own way to do it. Here it is.

MySQL script

Put this script at the top of your PHP file because it needs to store data first and then execute the script after.

<?php
if(isset($_POST['submit'])):
  $thedate=$_POST['dateyear']."-".$_POST['datemonth']."-".$_POST['dateday'];
  mysql_query("insert into income values('','".$thedate."')");
endif;
?>

Continue reading How to Store Date from Select HTML Tag to MySQL

The Difference Between get_posts() and query_posts() in WordPress

Many WordPress theme developers get confused by get_posts() and query_posts() functions at the first time they faced it.

get_posts() description: This is a simple function for creating multiple loops. It retrieves a list of latest posts or posts matching criteria.

query_posts() description: query_posts() can be used to control which posts show up in The Loop… The query_posts() function is intended to be used to modify the main page loop only. It is not intended as a means to create secondary loops on the page.

Yes the difference is about multiple loops and single loop.

Continue reading The Difference Between get_posts() and query_posts() in WordPress

How to Order MySQL Data Ascending and Descending Based on Field

The syntax is easy to remember.

SELECT * FROM <table> ORDER BY <field> <ASC/DESC>

Example:

  1. mysql_query("SELECT * FROM groceries ORDER BY shopname ASC")
  2. mysql_query("SELECT * FROM groceries ORDER BY shopaddress DESC")
  3. mysql_query("SELECT * FROM students ORDER BY department ASC")

If you don’t want to order it ascending or descending just remove ASC or DESC. It will be automatically ordered by the first data/record.