Thursday, 22 March 2012

Custom select dropdown with CSS and JS

Dropdowns have been a major pain in the backside in the past when it comes to styling of them, the default look doesn't always suite your website theme.


Customization is very limited, but it's not very difficult to change the style of the dropdown with CSS and javascript.


Lets turn this:






Into this:






Step 1
The HTML - This is simple, basically we have a div which holds the select element, it also holds the span element:


<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>


Step 2
The CSS - this tells the select element to become invisible, and tells the span what to look like and to sit below the selectbox.


<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>


Perfect, as you can see from the CSS, the span with the id of #selector uses a background image called dropdown.jpg - this gives the coloured background and the arrow, you can make your own one. - this image is 215x30px if you change the size of it make sure you adjust the CSS accordingly, basically fiddle around with the #selector width and padding. You may also need to change the .selectbox width and padding if you experience difficulty clicking the dropdown once you've changed the size.


Step 3
The Javascript - this is just to update the span with the selected dropdown option.


You can do this with the jQuery library (if you already use it on your website), or using built in javascript, both work fine:


jQuery


<script>
function changeValue(val)
{
 $('#selector').html(val);
}
</script>

Javascript


<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>


So there you have it, the complete code is below:


Complete Code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Custom Select box</title>
<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>
<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>
</head>

<body>
<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>
</body>
</html>

Wednesday, 14 March 2012

How to secure HTML5 video

With videos being used everywhere, more and more websites are trying to accommodate videos on their website, if it's for visual tutorials, or marketing. Sometimes though you don't want your video being downloaded and shared with others.. A good case for this would be a subscription based website.

Easy, lets use a flash player.. well ok, we could do.. but with more and more websites jumping over to HTML5 compliant, it would be nice if we could protect our videos and still use the HTML5 player.

The key issues with using an HTML5 player is there is absolutely NO encryption or protection for the videos.

Here's an example of the HTML5 video tag

<video controls>
    <source src="/sample.mp4" type="video/mp4" />
    <source src="/sample.webm" type="video/webm" />
</video>
All a viewer has to do is right click on the playing video and hit a button called 'Save Video As' and they'll have it on their computer. If not they can see exactly where the video is stored and download it anyway.

Protecting the video is a very complex method using multiple checks on multiple technologies, i won't go into the details..

We have spent hours, ounces (of coffee), an unmeasurable amount of stress developing this HTML5 protection.

After a lot of agressive testing we have secured our videos and have developed a very smart detection system which adapts to different download attempts.

And so from the creators of The Skyblue Transporter and The Custom Google +1 button we are proud to announce that we will be releasing Version 1 of our SkyblueVideoProtection (SkyBVP) on a cloud based subscription where you can securely store and stream your videos embedded directly to your website.. For more information please enquire!

Thursday, 13 October 2011

PHP: Getting disk usage (class)

I thought I might as well post this class I wrote a while ago. It basically gathers the following for the specific drive you've selected;
  • Raw Total Size
  • Raw Free Size
  • Readable Total Size
  • Readable Free Size
  • Percentage Free
  • Percentage Used
The Class
<?php
class diskSpace
{
 function __construct( $disk = '.' )
 {
  $this->the_drive = $disk;
  $this->raw_diskspace = $this->disk_spaces( "total" );
  $this->raw_freespace = $this->disk_spaces( "free" );
  $this->readable_diskspace = $this->readableSizes( $this->raw_diskspace );
  $this->readable_freespace = $this->readableSizes( $this->raw_freespace );
  $this->percentage_free = $this->percentages( "free" );
  $this->percentage_used = $this->percentages( "used" );
 }
 
 public function disk_spaces( $type )
 {
  switch($type)
  {
   case "total":
    return disk_total_space( $this->the_drive );
   break;
   case "free":
    return disk_free_space( $this->the_drive );
   break;
  }
 }
 
 public function readableSizes( $size )
 {
  $types = array( ' B', ' KB', ' MB', ' GB', ' TB', ' TB', ' EB', ' ZB', ' YB' );
  $i=0;
  while($size>=1024)
  {
   $size/=1024;
   $i++;
  }
  return("".round($size,2).$types[$i]);
 }
 
 public function percentages( $type )
 {
  switch($type)
  {
   case "free":
    return (round($this->raw_freespace / $this->raw_diskspace, 2) * 100) . "%";
   break;
   case "used":
    return round(100 - $this->percentage_free) . "%";
   break;
  }
 }
}
?>
The Usage
<?php
// load class with default drive
$dUsage = new diskSpace;

// or load class with specific drive
$dUsage = new diskSpace( "." );
// how to call each option
print("The Disk: " . $dUsage->the_drive);
// will output: .

print("Raw Size: " . $dUsage->raw_diskspace);
// will output: Raw Size: 489616760832

print("Raw Free: " . $dUsage->raw_freespace);
// will output: Raw Free: 454473097216

print("Readable Size: " . $dUsage->readable_diskspace);
// will output: Readable Size: 455.99 GB

print("Readable Free: " . $dUsage->readable_freespace);
// will output: Readable Free: 423.26 GB

print("Percentage Free: " . $dUsage->percentage_free);
// will output: Percentage Free: 93%

print("Percentage Used: " . $dUsage->percentage_used);
// will output: Percentage Used: 7%
?>

Wednesday, 12 October 2011

Make script not directly accessible

So if you're a PHP developer and want to make some include files protected. Basically so no one can look directly at them for whatever reason, simply;

<?php
if(preg_match("/".basename(__FILE__)."/i",$_SERVER['REQUEST_URI']))
{
 die("Direct access is not allowed");
}
?>

Simples!

Tuesday, 23 August 2011

Tutorial: How to apply a custom google plus +1 share button

So I needed a custom google+1 button for a website I am developing.. But there wasn't any around! Sure there were custom buttons, icons etc, but no way to implement them. I searched on the interwebz for a tutorial on how to make a custom google +1 share button.


But then I decided I might as well just make it myself.


It was surprisingly easy to be honest, and because of the lack of tutorials out there, I thought I'd write a quicky!


So, How did I turn this:







Into this: 







Well when you see below you'll kick yourself if you haven't already, because it's so simple.


Step 1
Pour yourself a brew, Now this can be either Tea or Coffee dependent on your preference.

Step 2 
The HTML 
<div class="googleplus">
    <div class="googlehider">
        <g:plusone annotation="none"></g:plusone>
        
        <!-- Place this render call where appropriate -->
        <script type="text/javascript">
          window.___gcfg = {lang: 'en-GB'};
        
          (function() {
            var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
            po.src = 'https://apis.google.com/js/plusone.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
          })();
        </script>
    </div>
    <img src="images/googleplus.png" class="mygoogle" />
</div>
Step 3 
Next, you'll have to get your custom icon, this is the one we are going to replace the original google +1 icon with. (Here's some good ones: http://www.iconfinder.com/search/search/?q=google%20plus
Make sure you name it googleplus.png and place it in your images folder.. - You may need to change the HTML above if your image is named or located elsewhere.


Step 4
The CSS
 
.googleplus {
 position:relative;
    /* - use this to position your share icon on your page -  */
}
.googlehider {
 opacity:0;
 height:30px;
 width:30px;
 position:absolute;
 top:0;
 left:0;
 z-index:3;
 -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
 filter:alpha(opacity=0);
}
.mygoogle {
 position:absolute;
 top:0;
 left:0;
 z-index:2;
 margin-left:6px;
}
Step 5 
Put it all together..

Step 6
Enjoy your freshly made brew.. it should still be warm ;-)

Walla! I hope this helps :-) 

Thursday, 26 May 2011

How to remove the fonts.com footer banner

If you use webfonts.fonts.com for their custom web fonts API, and like me, you don't like their default footer banner.
You've probably wanted to remove it and replace it with your own custom, much slicker banner which works nicely with your website.
I recently had this problem and decided to do something about it.

How they do it
The javascript file you include in the head of your html document loads their custom css and font, it then waits for 6 seconds to load the banner, this ensures all your other scripts have loaded, leaving nothing left but their banner to be placed neatly at the bottom of the page looking hideous.

I am going to give two examples here to removing this banner, the first will be using the jQuery library.

Using the jQuery library
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner.

/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});

This is an example of how to implement this into your webpage.
First of all you need to download the jQuery library from here jquery.com (latest version).
Secondly you need to make sure you place this inside your head tags
<html>
<head>
<title>My test page hiding fonts.com banner</title>
<script src="javascript/jquery.1.5.1-min.js" type="text/javascript"></script>
<script type="text/javascript">
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});
</script>
<head>

and that's it.

 
Using javascript
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner. The difference between this and the example above is that you don't need the jQuery library loaded, you also need to place this at the bottom of your webpage, just before the closing body tag.

<script>
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

 var count,newcount,xDivs;
 xDivs =  document.getElementsByTagName('div');
   // count total divs loaded on the page
 count = xDivs.length;
 function checkwebFontDiv() {
    // count total divs loaded on the page
  newcount = xDivs.length;
    // if the total divs has increased by 1 then continue
  if(newcount == count+1){
     // hide the last div (the fonts.com banner div)
   xDivs[count].style.display='none';
    // otherwise check again
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   // start checking for the new div
 checkwebFontDiv();

</script>
</body>
</html>

That's it!
Any questions please comment.