Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

21 November 2010

SyntaxHighlighter: Beautify code presentation

SyntaxHighlighter is a flexible & extensible syntax highlighter built with JavaScript. It supports a wide range of languages from CSS, PHP, C, C++, SQL and more.

<?php

echo 'Hello World';

?>

SyntaxHighlighter offers ready-to-use themes an new ones can be created easily for a custom presentation.

It has support for 3rd party applications like Blogger, Wordpress, Joomla & many others.

Download SyntaxHighlighter here.

12 January 2010

Disable textbox resize in Apple Safari and Google Chrome

Safari and Chrome are 2 browsers that support resizing of the html textbox element. Though this is good in some instances, this can be really annoying too as it can break your beautiful layout.

To disable resizing of the textbox element, add the following css to your code:

textbox { resize: none; }

That's it!

05 December 2009

Compress javascript code with Google Closure Compiler

The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.

Download the Closure Compiler here.

Charles Marshall has written a cool web application that utilizes the Google Closure Compiler and lets you input raw Javascript code and outputs optimized Javascript code.

Closure Compiler

25 November 2009

A simple file download script in PHP

When you have a download link to a .zip / .exe file on your website, it normally ask to download the file to a location on the client-side's computer. For .txt / .pdf / .gif etc. files it normally opens in the browser itself, provided it has support for it.

If you ever need to force the file to be download, you can easily do this in PHP. Below is the script which will force all file specified to be downloaded to the client-side's computer rather than open in the browser itself:

<?php

$filename = $_GET['filename'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression')){
    ini_set('zlib.output_compression', 'Off');
}

$file_extension = strtolower(substr(strrchr($filename, "."), 1));

if($filename == ""){

?>
    <html>
        <head>
            <title>File Download</title>
        </head>
        <body>
            ERROR: No filename specified.
        </body>
    </html>
<?

    exit;

}elseif(!file_exists($filename)){

?>
    <html>
        <head>
            <title>File Download</title>
        </head>
        <body>
            ERROR: File not found.
        </body>
    </html>
<?

    exit;

}
switch($file_extension){
    case "pdf":
        $contentType = "application/pdf";
    break;
    case "exe":
        $contentType = "application/octet-stream";
    break;
    case "zip":
        $contentType = "application/zip";
    break;
    case "doc":
        $contentType = "application/msword";
    break;
    case "xls":
        $contentType="application/vnd.ms-excel";
    break;
    case "ppt":
        $contentType = "application/vnd.ms-powerpoint";
    break;
    case "gif":
        $contentType = "image/gif";
    break;
    case "png":
        $contentType = "image/png";
    break;
    case "jpeg":
    case "jpg":
        $contentType = "image/jpg";
    break;
    default:
        $contentType = "application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: ".$contentType);
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile($filename);
exit;

?>