Archive

Posts Tagged ‘javascript’

Fixing the Time

December 7th, 2008

Fixing the Time

function total(year) {
  var seconds = 365 * 24 * 60 * 60; //Normal Year;
  var plus = 24 * 60 * 60; // Leap year have one more day
  var leap = false;
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) leap = true;
  if (leap) seconds += plus;
  if (year == 1972) seconds += 2;
  if (year > 1972 && year < 1980) seconds += 1;
  if (year > 1980 && year < 1984) seconds += 1;
  if (year > 1988 && year < 1991) seconds += 1;
  if (year > 1991 && year < 1996) seconds += 1;
  if (year > 1996 && year < 1999) seconds += 1;
  if (year == 1985 || year == 1987 || year == 2005 || year == 2008) seconds += 1;
  document.write('Year: '+year+', Total of secods: '+seconds+'
');
}
total(1972);
total(1975);
total(1980);
total(1981);
total(2004);
total(2005);
total(2006);
total(2007);
total(2008);
total(2009);

Result:

Year: 1972, Total of secods: 31622402

Year: 1975, Total of secods: 31536001

Year: 1980, Total of secods: 31622400

Year: 1981, Total of secods: 31536001

Year: 2004, Total of secods: 31622400

Year: 2005, Total of secods: 31536001

Year: 2006, Total of secods: 31536000

Year: 2007, Total of secods: 31536000

Year: 2008, Total of secods: 31622401

Year: 2009, Total of secods: 31536000

Leap Second (http://en.wikipedia.org/wiki/Leap_second)

Leap Years (http://en.wikipedia.org/wiki/Leap_year)

develop, security, web , , ,

CSS and MSIE

September 1st, 2008

CSS and MSIE

MSIE (Microsoft Internet Explorer) CSS Hack:

.somediv {
    height: 15px; /* all browsers */
    #height: 20px; /* all IE Browser */
    _height: 25px; /* only IE6 Browser */
}

MSIE use conditional comments: (http://msdn.microsoft.com/en-us/library/ms537512.aspx)

 <!--[if IE 6]>
    <script src="js/iefix.js" type="text/javascript"></script>
    <link rel="stylesheet"  href="css/iefix.css" type="text/css"></style>
    <div> some html code </div>
<![endif]-->

In this example only if the Browser are IE6 add this code:

<script src="js/iefix.js" type="text/javascript"></script>
<link rel="stylesheet"  href="css/iefix.css" type="text/css"></style>
<div> some html code </div>

Old browsers (MSIE) support :hover, :active and :focus:
http://www.xs4all.nl/~peterned/csshover.html

<style type="text/css">
    body {
        behavior:url("csshover.htc");
    }
</style>

Make MSIE behave like a standards-compliant browser. It fixes many HTML and CSS issues and makes transparent PNG work correctly under IE5 and IE6. http://code.google.com/p/ie7-js/

web ,

Safe and Dirty Remote XMLHttpRequest

August 27th, 2008

Safe and Dirty Remote XMLHttpRequest Sometimes we need XMLHttpRequest work on diferents domains (cross domains) a simple solution, you need JavaScript and Perl:

The XMLHttpRequest(); example:

var req = new XMLHttpRequest() // IE7, Firefox, Safari, Opera
req.open('GET', 'http://localhost/cgi-bin/remote.pl?url='+url, false);
req.send(null);

To make the remote request work, use remote.pl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
use LWP::UserAgent;
use CGI qw(:standard);
print "Content-type: text/plain\n";
my $url = param ('url');
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0");
$req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'text/html');
$res =$ua->request($req);
if ($res->is_success) {
	$page = $res->content;
} else {
	$page = "error";
} print $page;

This is only a example:

The remote.pl could be much better implement (GET and POST methods, params and theirs values).

develop, web ,

JSC JavaScript Collection

August 24th, 2008

JSC JavaScript Collection

We need jsmin and gzip, compile the jsmin.c:

gcc -o jsmin jsmin.c

Script to generate a collection of the javascript files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
 
echo "JavaScript Collection"
 
  EXPECTED_ARGS=1
  E_BADARGS=65
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {path of the javascripts}"
  exit $E_BADARGS
fi
 
# Join the javascript scripts in one file
cat $1/*.js >> $1/.__fullscripts.tmp
 
# use JSMIN to minifier the script
cat $1/.__fullscripts.tmp | jsmin > $1/.__fullscripts.min.tmp
 
# awk remove the first blank line, gzip compress the script
awk 'FNR>1{print}' $1/.__fullscripts.min.tmp | gzip > $1/collection_scripts.js.gz
 
# remove temporal files
rm $1/.__fullscripts.tmp $1/.__fullscripts.min.tmp

Howto: jsc public_html/web/js/

Now create one php script to replace all javascripts:

<script type="text/javascript" src="collection_scripts.php"></script>

Content of collection_scripts.php:

<?php
    header("Content-type: text/javascript; charset: UTF-8?");
    header("Content-Encoding: gzip");
    readfile("collection_scripts.js.gz");
?>

unix/linux , ,

Insert Any Video Of YouTube

August 23rd, 2008

Insert Any Video Of YouTube

I made this little Javascript function, this function insert any youtube video on a <div>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Script: YouTubeVideo Version 1.0 (2008/07/22)
// Author: Alberto Isaac Ayala Esquivias (Albertux)
// Licence: http://creativecommons.org/licenses/by-nc-sa/3.0/
// FeedBack to: albertoi7@gmail.com
// About: Insert youtube video on a div doesn't matter if embed code is disable this script insert the video.
 
function YouTubeVideo(id,url) {
 
	var video_html ='<object width="425" height="344"><param name="movie" value="http://www.youtube.com/swf/l.swf?video_id=VIDEO&rel=1&iurl=null&autoplay=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/swf/l.swf?video_id=VIDEO&rel=1&iurl=null&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>';
 
	var n = url.indexOf('v=');
 
	var id_video = url.substring(n+2,n+14);
 
	document.getElementById(id).innerHTML=video_html.replace(/VIDEO/g, id_video);
 
}

Howto use:

<input type="button" onclick="YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0');" value="Watch  Video">
 
<div id="contenedor"></div>
  

On load page:

// JavaScript onload event example:
window.onload = function() { YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0'); }
 
// jQuery example:
$(document).ready(function() {
	YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0');
});

develop, web ,

Google Maps Api

July 9th, 2008

Google Maps Api

Search Albertux’s house …

La Api de Google Maps nos sirve para agregarles funcionalidades a los mapas, en mi caso hice este ejemplo de localización de mi casa de Cadereyta, aveces en algunos proyectos de sistemas se requiere de la geolocalización de propiedades o ubicación de inmuebles.


develop ,

Class Generator

June 26th, 2008

Class Generator

Ayer en la noche estaba aburrido hice un generador de clases muy básico de momento solo implementa javascript, php4, php5 y python.

(http://ayalasoft.com/utils/genclass/)

develop , , ,