Posts

Showing posts with the label Javascript

Word Counter in javascript

Image
Before writing this post,when i have searched for the word counter in javascript i found so many scripts that are not up to the mark.The script was like below. function countwords(textarea){  var words=textarea.split(" ");  alert(words.length+" words"); } This script is wrong.You are having question why its wrong?.Because splitting with space is used in the above script.Its not the only thing to separate the words. A newline will be inserted if there is Return at the end of the word, which won't be counted as a word separator by the above code.commas or other punctuations  which are not followed by a space are also not handled in the above script.Even if you leave a space at the end of the word it will also counted as a word that is totally wrong. So what could be the right solution for this case? As a solution, in the following script we are using regular expression.The pattern '\w+'used here will match a srting of characters of the word.Using this scri...

Simple Tabs Using Javascript and CSS

Image
For the content-crowded web sites tabs or tabbing can be very useful. Plenty of space can be saved by using tabs. I mean just by placing content block in tabs and displaying only one block at a time. Few days back I had a deal with tabbing. It would be better if I say not only tabbing but also very complex tabbing having awesome effects created using complex coding of jquery and nice look. Many hours were spent for its creation. After its completion, I realized that there should be a very simple demo that acts like a row material for such complex tabbing creation that i had created. So, i have created very simple tabbing using javascript and css. Download Demo Features of this Demo => Very easy to understand => Only simple Javascript used => Highly customizable => Easy to implement. Download the Demo ,implement it in your projects and customize it as per your requirements. If you find this post helpful then share it.Please have a look at other similar kind of post Here .

For or Foreach? PHP vs. Javascript, C++, Java, HipHop (update: HHVM v2)

Lessons learned: Foreach is 4-5 times faster than For Nested Foreach is 4-5 times faster than nested For Foreach with key lookup is 2 times slower than Foreach without C++ is 60 times faster than PHP running For/Foreach on Arrays Javascript is 2-20 times slower than C++/Java running For on Arrays HipHop is currently no alternative to C++ Using a better CPU makes the code 4-5 times faster Here is a sample script, running on a 1.4 GHz machine with PHP 5.4.0: // init arrays $array = array(); for ($i=0; $i<50000; $i++) $array[] = $i*2; $array2 = array(); for ($i=20000; $i<21000; $i++) $array2[] = $i*2; // test1: foreach big-array (foreach small-array) $start = microtime(true); foreach ($array as $val) { foreach ($array2 as $val2) if ($val == $val2) {} } echo (microtime(true)-$start)."\n"; // test1b: foreach big-array (foreach small-array) $start = microtime(true); foreach ($array as $val) { foreach ($array2 as $val2) if ($val === $val2) {} } echo (microtime(true)-$st...

Characters Counter in Javascript

Image
You may have seen characters counter while sending messages from online free message services or in some cases while filling up online registration forms.In this article you will learn how to create character counter in javascript. Javascript <script type="text/javascript"> //You can change the counter/limiter value as your wish var count = "125";   function limiter(){ var textvalue = document.demoform.message.value; var len = textvalue.length; if(len > count){ tex = textvalue.substring(0,count); document.demoform.message.value =tex; return false; } document.demoform.limit.value = count-len; } </script>  HTML Code <html> <body> <form name="demoform" method=”post”> <table><tr><td><strong>Characters Counter Demo</strong></td></tr> <tr><td colspan="2"><textarea name=message rows=3 cols=40 onkeyup=limiter()></textarea></td></tr> <tr...

Using V8 Javascript engine as a PHP extension (update: write PHP session)

"We Are Borg PHP. We Will Assimilate You. Resistance Is Futile!" Just got to something described as: This extension embeds the V8 Javascript Engine into PHP. It is called v8js and the documentation is already available on php.net , examples and the sources are here . V8 is known to work well in browsers and webservers like node.js, but does it work inside PHP? YES! Here is the installation on Ubuntu 12.04: sudo apt-get install php5-dev php-pear libv8-dev build-essential sudo pecl install v8js sudo echo extension=v8js.so >>/etc/php5/cli/php.ini sudo echo extension=v8js.so >>/etc/php5/apache2/php.ini php -m | grep v8 Let's run a small test script: <?php $start = microtime(true); $array = array(); for ($i=0; $i<50000; $i++) $array[] = $i*2; $array2 = array(); for ($i=20000; $i<21000; $i++) $array2[] = $i*2; foreach ($array as $val) { foreach ($array2 as $val2) if ($val == $val2) {} } echo (microtime(true)-$start)."\n"; // 8.60s $star...

Dynamic CSS insertion with Javascript

最近碰到的需求,在沒有 server side script supported 的前提下,動態載入 CSS。 function appendCSS() { var headID = document.getElementsByTagName("head")[0]; var cssNode = document.createElement("link"); cssNode.type = "text/css"; cssNode.rel = "stylesheet"; cssNode.media = "screen"; cssNode.href = "css/style.css"; headID.appendChild(cssNode); } 只要再搭配個判斷 browser 版本的 function 就可以簡單實現根據不同的 browser 切換對應 CSS 的目的了。