Skip to content

Instantly share code, notes, and snippets.

@enopanen
enopanen / animate_template
Created December 3, 2013 18:01
As we saw the animate method earlier, this is very powerful for creating dynamic movement on your page. CSS3 transitions are a whole lot easier in some circumstances. But with animate you can manipulate multiple objects or CSS properties all at once! It’s a very powerful library to get into and start playing with. If you haven’t used any of the …
$('p').animate({
left: '+=90px',
top: '+=150px',
opacity: 0.25
}, 900, 'linear', function() {
// function code on animation complete
});
@enopanen
enopanen / AJAX_Template
Created December 3, 2013 18:00
Passing form data via Ajax is one of the most common uses for jQuery. As a web developer myself I can’t think how many times I am using the ajax method in each project. The syntax can be awfully confusing to memorize, and checking the documentation gets old after a while. Consider copying this small template for usage in any future Ajax-powered …
$.ajax({
type: 'POST',
url: 'backend.php',
data: "q="+myform.serialize(),
success: function(data){
// on success use return data here
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type "+type);
@enopanen
enopanen / Scroll_to_Top
Created December 3, 2013 18:00
You have probably seen this functionality becoming popular on all the new social networking websites. I have definitely seen this technique appear on infinite-scrolling layouts such as Tumblr and Pinterest. The code is very minimal but powerful in some layouts. You are offering a simple link or button display which behaves like a “back to home” …
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
@enopanen
enopanen / preventDefault
Created December 3, 2013 17:59
When you create JavaScript applications there are plenty of times where you need a link or button to just do nothing. This is often for triggering some type of dynamic effect, such as a hidden menu or Ajax call. By using the event object during any click, we can manipulate the data sent back to the browser URL. In this scenario I am stopping the…
$("a").on("click", function(e){
e.preventDefault();
});
@enopanen
enopanen / hover_toggle
Created December 3, 2013 17:58
The jQuery hover method is a quick solution for handling these events. You can determine whether the user is just hovering over your element, or if the user is just leaving the hover state. This allows for two custom functions where you can run two distinct sets of code.
$("a").hover(
function () {
// code on hover over
},
function () {
// code on away from hover
}
);
@enopanen
enopanen / Compress_with_gzcompress
Created December 3, 2013 16:47
Compress data using gzcompress() When working with strings, it is not rare that some are very long. Using the gzcompress() function, strings can be compressed. To uncompressed it, simply call the gzuncompress() function as demonstrated below:
$string =
"Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam, consectetur vitae
@enopanen
enopanen / Display_Source_Code
Created December 3, 2013 16:46
Display source code of any webpage Want to be able to display the source code of any webpage, with line numbering? Here is a simple code snippet to do it. Just modify the url on line 2 at your convenience. Or even better, make a pretty function according to your needs.
<?php // display source code
$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) {
// loop thru each line and prepend line numbers
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
}
@enopanen
enopanen / Text_with_TextMagic
Created December 3, 2013 16:45
Text messaging with PHP using the TextMagic API If for some reason, you need to send text messages to your clients cell phones, you should definitely have a look to TextMagic. They provide an easy API which allow you to send SMS to cell phones. Please note that the TextMagic service isn’t free. The example below shows how easy it is to send a SM…
// Include the TextMagic PHP lib
require('textmagic-sms-api-php/TextMagicAPI.php');
// Set the username and password information
$username = 'myusername';
$password = 'mypassword';
// Create a new instance of TM
$router = new TextMagicAPI(array(
'username' => $username,
@enopanen
enopanen / Convert_Currency
Created December 3, 2013 16:44
Convert currencies using cURl and Google Converting currencies isn’t very hard to do, but as the currencies fluctuates all the time, we definitely need to use a service like Google to get the most recent values. The currency() function take 3 parameters: from, to, and sum.
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
@enopanen
enopanen / Download_All_Images_From_Webpage
Created December 3, 2013 16:43
Download and save images from a page using cURL Here is a set of functions that can be very useful: Give this script the url of a webpage, and it will save all images from this page on your server.
function getImages($html) {
$matches = array();
$regex = '~http://somedomain.com/images/(.*?)\.jpg~i';
preg_match_all($regex, $html, $matches);
foreach ($matches[1] as $img) {
saveImg($img);
}
}
function saveImg($name) {