Category Archives: javascript

Working with select

We have the html code below: <select name="sel" id="sel"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> Code below set the current value

Posted in javascript | Leave a comment

How to select option when page is loaded

<html> <head> <script type=”text/javascript”> function load() { document.getElementById(‘select_id’).value = 24; } </script> </head> <body onload=”load()”> <h1>Hello World!</h1> </body> </html>

Posted in javascript, javascript snippets | Tagged | Leave a comment

Include JQuery UI framework from Google libs

Code snippet to include JQuery UI framework from Google libs &lt;head&gt; &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js&quot;&gt;&lt;/script&gt; &lt;link href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;/&gt; &lt;/head&gt;

Posted in javascript, javascript snippets, jquery, jquery snippets | Tagged | Leave a comment

Javascript function to round number to 2 decimal places

function roundNumber(num, dec) { var result = String(Math.round(num*Math.pow(10,dec))/Math.pow(10,dec)); if(result.indexOf(‘.’)<0) {result+= ‘.’;} while(result.length- result.indexOf(‘.’)<=dec) {result+= ’0′;} return result; }

Posted in javascript, javascript snippets | Tagged | Leave a comment

Javascript function to get curent date

Here is the implementation of javascript function returning the current date in dd-mmm-yyyy format function getCurDate(){ var months = new Array(“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Des”); var myDate = new Date(); return myDate.getDate() + … Continue reading

Posted in javascript, javascript snippets | Tagged | Leave a comment

Javascript: how to detect if variable is defined

if( “undefined” === typeof data ) alert(‘data is not defined); else alert(‘data is not defined’);

Posted in javascript, javascript snippets | Tagged | Leave a comment

How to send the variable from php to html page

&lt;script type=&quot;text/javascript&quot;&gt; $(document).ready(function(){ var s=location.hash; var arr=s.split('-'); if( arr[1] ) alert( &quot;Please enter the &quot; + arr[1] );

Posted in javascript, php | Leave a comment

Javascript: how to use history back() method

&lt;html&gt; &lt;head&gt; &lt;script type=&quot;text/javascript&quot;&gt; function goBack() { window.history.back() } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type=&quot;button&quot; value=&quot;Back&quot; onclick=&quot;goBack()&quot; /&gt; &lt;/body&gt; &lt;/html&gt;

Posted in javascript, javascript snippets | Leave a comment

How to implode array in JavaScript

var arrayName = new Array(); arrayName.join(‘delimiter’);

Posted in javascript, javascript snippets | Tagged | Leave a comment

Javascript: How to resize the image to content width

&lt;script&gt; $('#article_page_{$article_id}').live( 'pagecreate',function(event) { var my_w=$('#article_page_{$article_id}').width() – 40; $('.article_text img').each( function(){ $(this).css('width', ($(this).width() &gt; my_w?$(this).width():my_w) +'px'); }); }); &lt;/script&gt;&quot;;

Posted in javascript, javascript snippets, jquery | Leave a comment