Marius van Witzenburg We fight for our survival, we fight!

15jun/110

jQuery its ‘document ready’ can be written on different ways.

Posted by mariusvw

I have seen a lot of scripts already still using a old way of checking if the DOM is ready for action like this:

1
2
3
4
5
window.onDomReady = myFunction;
 
function myFunction() {
    alert('hello');
}

or (which is not smart cause onload waits for even the images to be loaded):

1
2
3
4
5
window.onload = myFunction;
 
function myFunction() {
    alert('hello');
}

jQuery has a nicer way to implement this with the following piece of code:

1
2
3
$(document).ready(function() {
// your code here
});

However you can write this even a bit shorter than most people already do by using the above code, like this:

1
2
3
$(function() {
// your code here
});

I hope you find this useful, make something nice with jQuery! ;-)

14jun/110

How to control a Flash movie from Javascript and use Javascript from Flash movies

Posted by mariusvw

Personally I dislike the usage of Flash in my websites but our clients request a lot that they want animations. Now it happens that they want the animation to act upon an action to a non-flash object. This can be difficult if you have no idea how to do that. If you read below, you should have no problem with getting this job done :-)

Control your Flash Movie from Javascript:

// Play movie
document.getElementById('myFlashMovie').Play();
// Stop movie
document.getElementById('myFlashMovie').StopPlay();
// Rewind movie to beginning
document.getElementById('myFlashMovie').Rewind();
// Goto next frame of MovieClip
document.getElementById('myFlashMovie').TGetProperty(nameOfTargetMovieClip, propertyIndex);
// Goto next frame of TimeLine
document.getElementById('myFlashMovie').GotoFrame(frameNum);
// Zoom in or out the flash movie
document.getElementById('myFlashMovie').Zoom(relative percentage);
// Set variable and make it available via ActionScript
document.getElementById('myFlashMovie').SetVariable(variableName, variableValue);
// Get variable for usage in JavaScript
var myVariable = document.getElementById('myFlashMovie').GetVariable(variableName);

To control Javascript functions you can call it like this:

ActionScript 1/2

getURL("javascript:name_of_your_function();");

ActionScript 3 direct call

var url:String = "javascript:name_of_your_function();";
var request:URLRequest = new URLRequest(url);
try {
  navigateToURL(request);
} catch (e:Error) {
  trace("Error occurred!");
}

ActionScript 3 event mouse click call

movieClipName.addEventListener(MouseEvent.CLICK, callLink);
function callLink:void {
  var url:String = "javascript:name_of_your_function();";
  var request:URLRequest = new URLRequest(url);
  try {
    navigateToURL(request);
  } catch (e:Error) {
    trace("Error occurred!");
  }
}

For some more information about these methods, please check out the Adobe website.

I hope this helps you out with getting that Flash Movie behave like you want it to ;-)

12jun/110

How to get parameters from query string with JavaScript

Posted by mariusvw

I don't remember where I found this piece of code, but it works nice to get query string parameters :-)

function getURLParam(strParamName) {
	var strReturn = '';
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++) {
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}

it works quite simple, first it grabs all data after the question mark in the url, then it splits this result on the & character and then it splits again on the equal character. The result of that exists out of two pieces, the first is the key and the second the value of that key.

12jun/110

Count words or characters in a textarea with JavaScript

Posted by mariusvw

If you don't want to use a framework like jQuery and just script with native JavaScript this might become handy to use.

Check the following sites:

Words
http://www.javascriptkit.com/script/script2/countwords.shtml

Characters
http://www.javascriptkit.com/script/script2/charcount.shtml

Good luck :-)

Geëtiketeerd als: , , Geen reacties
25aug/100

How to fix inconsistent line ending (EOL) style with find and Perl

Posted by mariusvw

If you work with subversion you might get this error when you got files that have been edited on different operating systems like Windows, Linux, FreeBSD or Mac OS X.

Well, the fix is quite simple. You simply replace the wrong line endings with right ones depending of which you want. In my situation I want unix style line endings.

Replace in PHP/JavaScript files:

find ./ -name '*.php' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.php' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

In case you want to replace them in multiple file types you can adjust the command. In this example we want to replace in the following file types:

  1. asp
  2. cfm
  3. css
  4. html
  5. js
  6. php
  7. pl
  8. txt

Use the following commands:

find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

Keep in mind, you may use these commands on your own risk. I'm not responsible if you lose your work ;-)

Now you should be able to commit your files again :-)

15aug/090

A handy bookmark for FreeBSD manual pages

Posted by mariusvw

Add a bookmark to your browsers toolbar and add the following javascript as link:

javascript:var%20reply%20=%20prompt("What%20are%20you%20searching%20for?",%20"");if%20(reply%20==%20undefined)%20{%20return%20false;%20}%20document.location%20=%20"http://www.freebsd.org/cgi/man.cgi?query="%20+%20reply;

Now you can easy query the FreeBSD manpages from your browser without having to remember the URL.