28
Aug '13

You won’t often find me posting an IE only chunk of code here but today I’m making an exception as I have found myself writing a site specifically for use in a call-centre where I know for a fact that everyone will be using IE so I can make a few custom nice-to-haves with that in mind.

IE, for all its faults, is the only browser that will let you implement a copy to clipboard function without the need for Flash plugins. There are lots of tutorials out there that show you ways to do this, but I ended up building a very quick jQuery function that I thought I’d share.

This assumes that you have a container with a class of ‘copy_text’ and a button, link or other clickable element with the class of ‘copy_to_clipboard’:

$('.copy_to_clipboard').click(function(e) {
    e.preventDefault();

    if (!document.all)
        return; // IE only

    var copy_text = $(this).siblings('.copy_text').html();
    var new_element = document.createElement('input');
    new_element.setAttribute('type', 'text');
    new_element.setAttribute('class', 'copy_text_dynamic');
    new_element.setAttribute('value', copy_text);
    document.body.insertBefore(new_element)

    r = new_element.createTextRange();
    r.execCommand('copy');
});

Now you’ll notice this has created an input element on the fly. This is because IE’s createTextRange call only works on input elements so we have to create one and fill it with the text that we want to copy to the clipboard.

The final part of this process is making sure that this newly created input element doesn’t show up on our page. You may have already noticed that you can’t hide an element (i.e. display: none) in IE if you want to do anything with it as it doesn’t bother putting hidden elements in the DOM. That being the case, as an alternative I prefer to fix the position of the element and send it way off the side of the screen like so:

.copy_text_dynamic {
    display: block;
    position: fixed;
    left: -10000px;
}
02
Aug '13

PHP has three key catch (or ‘method missing’) functions, used when calls are made to class methods or parameters that don’t exist. These can be incredibly useful and time-saving if used properly, and cause incredible headaches if used badly!

__call()

This catches calls to any method names that don’t exist in the current class, or any (non-private) methods any parent classes. The called method name will be passed as the first argument and any arguments will be wrapped in an array and passed as the second argument. I have found this to be particularly useful to save myself the creation of getter and setter methods on my classes – a quick match against the first three letters can tell me if it’s a get or a set that has been called and I can then perform the operation on whatever structure actually holds the data I’m after.

__get() and __set()

These are used as overrides for calls to get and set class variables directly e.g. $class->variable
I find them particularly useful to provide shortcuts to getter and setter methods so the methods can be used as ‘virtual’ class variables e.g.

public function __get($key) {
    $method_name = 'get' . ucfirst($key);
    return (method_exists($this, $method_name)) ? $this->$method_name() : false);
}
public function __set($key, $value) {
    $method_name = 'set' . ucfirst($key);
    return (method_exists($this, $method_name)) ? $this->$method_name($value) : false);
}