Register now and start sharing your code snippets.
-->

Simple JavaScript countdown timer

JavaScript posted about 1 month ago by christian

This JavaScript displays the days, hours, minutes and seconds left to the given date:

   1  function Countdown(then) {
   2  
   3  	this.then = then;
   4  	
   5  	function setElement(id, value) {
   6  		if (value.length < 2) {
   7  	    	value = "0" + value;
   8  		}
   9  	
  10  		window.document.getElementById(id).innerHTML = value;
  11  	}
  12  	
  13  	function countdown() {
  14  		now  		  = new Date();
  15  	  	diff		  = new Date(this.then - now);
  16  	  	
  17  		seconds_left  = Math.floor(diff.valueOf() / 1000);
  18  	
  19  		seconds  = Math.floor(seconds_left / 1) % 60;
  20  		minutes  = Math.floor(seconds_left / 60) % 60;
  21  		hours    = Math.floor(seconds_left / 3600) % 24;
  22  		days     = Math.floor(seconds_left / 86400) % 86400;
  23  		
  24  		setElement('countdown-days', days);
  25  		setElement('countdown-hours', hours);
  26  		setElement('countdown-minutes', minutes);
  27  		setElement('countdown-seconds', seconds);
  28  		
  29  		countdown.timer = setTimeout(countdown, 1000);
  30  	}
  31  	
  32  		
  33  	function start() {
  34  		this.timer = setTimeout(countdown, 1000);
  35  	}
  36  	
  37  	start(then);	
  38  }
  39  
  40  Countdown(new Date("Dec 04 2008 12:00:00"));

Required HTML:

   1  <span id="countdown-days"></span> days
   2  
   3  <span id="countdown-hours"></span>:<span id="countdown-minutes"></span>:<span id="countdown-seconds"></span>

Output is for example:

   1  23 days 23:00:12

Tagged countdown, timer, javascript

How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"

Ruby posted 7 months ago by christian

All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.

This is a slight variation of his code:

In application.html.erb, or whatever layout file you’re using, put:

   1  <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>

In application.js, or whatever JavaScript file you’re using, put:

   1  $(document).ajaxSend(function(event, request, settings) {
   2    if (typeof(window.AUTH_TOKEN) == "undefined") return;
   3    settings.data = settings.data || "";
   4    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
   5  });

That’s all…

Tagged authenticity, rails, rails 2, token, jquery, javascript, ajax

Opening a new window in the center of the screen with JavaScript

JavaScript posted 9 months ago by christian

This JavaScript code will open a URL in a new window and center the window on the screen, instead of showing the window in a random place:

   1  var url = 'http://google.com/';
   2  var width  = 640;
   3  var height = 480;
   4     
   5  var left   = (screen.width / 2) - (width / 2);
   6  var top    = (screen.height / 2) - (height / 2);
   7     
   8  var xWindow = window.open(url, 'post', 'resizable=1,width=' + width + ',height=' + height + ', top=' + top + ', left=' + left);
   9  
  10  xWindow.focus(); 

Tagged javascript, window, open, center, screen

Combining CSS and JavaScript files with PHP to reduce page load time

PHP posted 9 months ago by christian

This is a very simple utility class that I created which combines multiple files into one—in contrast to all other PHP scripts I’ve found elsewhere, which all work badly and are a million lines long, this one is simple to understand and maintain:

   1  <?php
   2  class Files
   3  {
   4  	private static function process($files, $output, $base_dir = '')
   5  	{
   6  		$contents 	= '';
   7  		
   8  		# If this is a string then it's a search pattern
   9  		if(is_string($files))
  10  		{
  11  			$files = glob($files);
  12  		}
  13  		
  14  		foreach ($files as $file) 
  15  		{
  16  			$contents .= file_get_contents($base_dir.$file) . "\n\n";
  17  		}
  18  	
  19  		# TODO locking 
  20  		if ($fp = fopen($output, 'wb')) 
  21  		{
  22  			fwrite($fp, $contents);
  23  			fclose($fp);
  24  		}
  25  	
  26  		return $contents;
  27  	}
  28  	
  29  	#
  30  	# Combine all CSS files in the css directory:
  31  	#
  32  	#	echo Files::combine("css/*.css", "css/all.css");
  33  	#
  34  	# Combine only specified files in the given order:
  35  	#
  36  	#	echo Files::combine(array("css/core.css", "css/default.css"), "css/all.css");
  37  	#
  38  	#
  39  	static function combine($files, $output_filename, $base_dir = '')
  40  	{
  41  		# Combine the files
  42  		if(!file_exists($output_filename))
  43  		{
  44  			self::process($files, $output_filename, $base_dir);
  45  		}
  46  	}
  47  }
  48  ?>

The call to Files::combine should go in a file that is called on each request. On the first request to this file, the files are combined into one, on subsequent requests nothing is done, the only overhead is a call to file_exists.

Ideally the name of the output filename should contain a version number, so that an updated CSS file is not taken from the browser cache. This can easily be supported by reading the build number from somewhere and appending it to the output filename:

   1  Files::combine("css/*.css", "css/all-v$version.css");

Now all you have to do is add this to your HTML :

   1  <link rel="stylesheet" type="text/css" href="/css/all-v<?php echo $version ?>.css" media="screen" />

This also works for JavaScript files. Note that I don’t make an effort to minify anything as GZIP usually does a great job at minimizing bandwidth costs.

Tagged css, javascript, combine, php

Disable javascript resizing of Firefox

Shell Script (Bash) posted 11 months ago by marko

There is absolutely not a single developer in the world who knows what size I like my browser, although many people think they do. To prevent resizing of FF make the following steps.

  1. Open browser preferences
  2. Choose “Content”
  3. Choose “Advanced” on the “Enable javascript” preference
  4. Untick “Move or resize existing windows”

Tagged resize, firefox, browser, koon muuttaminen, javascript, fönstrets dimensioner