Author Archives: Eoin
Uninstall and Remove Xcode Completely
If you are in a position where you want to remove the colossus that is Xcode, here is the terminal command to use.
sudo /Developer/Library/uninstall-devtools –mode=all
In my case, I was using migration assistant to migrate my applications and user account to a new laptop. My older laptop had an older version of Xcode that I didn’t want included in the migration and as it is so big, I didn’t want to take the chance.
HTML5 Data attributes in HTML and jQuery
When writing javascript, it is often necessary to include metadata in the HTML markup, to help define some element or behaviour. There are common options available. You can use hidden inputs and/or standard attributes like class or title to store this metadata. However with HTML5′s data attribute, storing and parsing this data has become a whole lot easier and cleaner.
The syntax is straightforward. Any attribute prefixed with data- will be treated as data storage.
<div class="test" data-foo="bar"></div>
jQuery accesses this data like so…
var data = $( 'div.test' ).data( 'foo' ); // returns bar
Support for the data attribute has been added since jQuery version 1.4.3. jQuery’s implementation is smart enough that it can parse the attribute easily and even determine the correct data type used.
What I have found really useful is the fact that the jQuery can parse JSON syntax and return a JSON object. This makes passing data in PHP trivial, using the json_encode method. We also need to use htmlspecialchars method to escape or convert any quotes in the JSON string.
<?php $test = array( 'row' => 1, 'col' => 6, 'color' => 'pink' ); //create array of data you want to pass to jquery $test = json_encode( $test ); //convert array to a JSON string $test = htmlspecialchars( $test, ENT_QUOTES ); //convert any quotes into HTML entities so JSON string behaves as a proper HTML attribute. ?> <div class="test" data-complex="<?php echo $test ; ?>"></div>
The jQuery parses the JSON string like so…
var test = $( 'div.test' ).data( 'complex' ); // returns JSON Object console.log( test.color ); // outputs pink!
Important to note that this method is also backward compatible with older browsers, so there is no excuse not to give it a go!
iWorks 09 DVD installation problems
I’m pretty sure this will help nobody and it’s unlikely I will have the same problem again but just in case…
If you are having difficulty installing iWorks 09 from the DVD, then this is what I went through to finally get it to work.
Continue reading
Filter IP addresses with PHP
You may at some stage want to filter an online service based on IP address. In other words, you may want to block or grant access to a request based on their IP address. This can be handled in PHP by doing the following.
If you have the IP addresses, then it is trivial.
//First check IP address is valid
$request_ip = $_SERVER['REMOTE_ADDR'];
if ( !preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $request_ip ) )
return false;
$blacklist = array(
'111.222.12.11',
'222.111.21.22',
'221.112.11.12'
);
//check that ip is not blacklisted
if ( in_array( $request_ip, $blacklist ) )
return false;
If you want to include a range of IP addresses, best to use a regular expression.
$blacklist_ip_range = array(
'/^122\.244\.(\d+)\.(\d+)/', //for IP address in the range 122.244.0.0 - 122.244.255.255
'/^123\.(\d+)\.(\d+)\.(\d+)/', //for IP address in the range 123.0.0.0 - 123.255.255.255
);
foreach( $blacklist_ip_range as $ip ) {
if( preg_match( $ip, $request_ip ) )
return false;
}
If you have a better solution, then please let me know.
Change Float style with Javascript
If you would like to change the float style of some element using javascript, then use the following.
var element = document.getElementById( 'some_element_id' ); element.style.cssFloat = 'left';
Mac mail not sending Gmail email
Infrequently my Mac mail would be rendered incapable of sending email.
It appears the Mac mail client could not connect to the outgoing Gmail SMTP server, smtp.gmail.com, then after a few hours the connection would be restored automatically.
I assumed that my settings must be fine as they remained constant, it must be a problem on the Gmail servers end, but it was happening too often for that to be plausible.
How to check if an element exists in jQuery
if ( $("#mydiv").length ){
// do something here
}
Briefer again…
if ( $("#mydiv")[0] ){
// do something here
}
via http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/
Faster Javascript Trim
Want the best way to trim a sting in javascript?
A while ago, I came across a post on such a subject. It was written a few years ago but it’s still as relevant and useful as ever.
Find process using port number in Bash
Something that has bugged me recently is that when I go to proxy on a particular port, the port is already in use and I get the following error.
bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 8090
Could not request local forwarding.
Continue reading