Archive for the ‘JavaScript’ Category.

Search suggestions in AJAX + PHP

Simple search suggestions for PHP using ajax.

HTML:

<input type="text" name="s" id="s" value="" onkeyup="showSugestions();" />
<div id="livesearch"></div>

Continue reading ‘Search suggestions in AJAX + PHP’ »

Moving option elements HTML


Not much to explain just an example:

Left

Right

Show me the code!
Continue reading ‘Moving option elements HTML’ »

Include Javascript file inside another

This can be really handy sometimes, in Javascript there’s no way of including or importing other JS files.

For example in my case I want it to include the JQuery file from the CDN of Google, but I didn’t want to use another typical call to an external file.

What I did is inside of the common JS library of my project add this:

var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://code.jquery.com/jquery-1.4.2.min.js';
head.appendChild(script);

With tihs simple trick we added the script to the HTML header.

Of course you can use a function if you are going to use it a lot:
Continue reading ‘Include Javascript file inside another’ »

Youtube brackets

I’ve been using youtube bracket for wp for long time.

Based on that Idea I isolated it to make it work at any installation.

This script allows you to write anywhere in your content this [youtube=http://www.youtube.com/watch?v=x89xAXHd2l8] and will return a flash player with the video embed.

Function that embeds the flash in the content:

$content=mediaPostDesc($content);//usage
 
function mediaPostDesc($the_content){//from a description add the media
//using http://www.robertbuzink.nl/journal/2006/11/23/youtube-brackets-wordpress-plugin/
    if (VIDEO){
        $stag = "[youtube=http://www.youtube.com/watch?v=";
        $etag = "]";
        $spos = strpos($the_content, $stag);
        if ($spos !== false){
            $epos = strpos($the_content, $etag, $spos);
            $spose = $spos + strlen($stag);
            $slen = $epos - $spose;
            $file  = substr($the_content, $spose, $slen);    
			//youtube
            $tags = '<object width="425" height="350">
                    <param name="movie" value="'.$file.'"></param>
                    <param name="wmode" value="transparent" ></param>
                    <embed src="http://www.youtube.com/v/'. $file.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed>
                    </object>';    
            $new_content = substr($the_content,0,$spos);
            $new_content .= $tags;
            $new_content .= substr($the_content,($epos+1));
 
            if ($epos+1 < strlen($the_content)) {//reciproco
                $new_content = mediaPostDesc($new_content);
            }
            return $new_content;
        }
        else return $the_content;
    }
    else return $the_content;
}

Continue reading ‘Youtube brackets’ »

Allow only numbers or letters in HTML inputs

What about forcing the user to type a number/letter in the input you want.

With just a bit of JavaScript we can control this what is the user typing int he input.

Allow only numbers:
(also dot and del)

function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
 
	if((charCode==46||charCode==8||charCode==45||charCode==47) ||(charCode >= 48 && charCode <= 57) ){
		return true;
	}
	else {
		return false;
	}		
}

And to use it in a input would be like this for example:

<input id="math"  type="text"  onkeypress="return isNumberKey(event);" />

Try it: How much is 20+10?

Allow only letters:
(also space, del, enter)

function isAlphaKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
	 if ((charCode==231 || charCode==199) || (charCode==241 || charCode==209) ||(charCode==8 || charCode==32) || ( (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) ) ) {
	 	return true;
	 }
	 else {
		 return false;
	 }
}

And to use it in a input would be like this for example:

<input id="name" type="text"  onkeypress="return isAlphaKey(event);"  />

Try it: Your name:

I know should be easier, but just wait a bit for HTML 5 that is going to make our life better. For sure ;)

Follow me