Javascript

Javascript Equivalent of PHP’s trim function

Trim function in PHP can remove whitespace from the beginning and end of a string. Javascript do not have this function, but is easy to made one. [code] // remove space in a string function trim(str) { if(!str || typeof str != 'string') return null; return str.replace('/^\s+|\s+$/g', ''); } [/code]

Javascript Equivalent of PHP’s explode() and implode()

Do you know javascript has equivalent functions as php's explode() and implode(). Let's say we have a string: [code] $string = 'aa:bb:cc'; [/code] We want to break down each number into an array called $num_array. In PHP we could do this: [code] $explode_array = explode(':', $string); [/code] The PHP implode function would produce the following: [code] $explode_array[0] = 13 $explode_array[1] = 42 $explode_array[2] = 43 $explode_array[3] = 56 [/code] In Javascrip...

How to add bookmark javascript in IE and Firefox

Adding the "bookmark this page" javascript below will help to encourage return visits. Your visitor simply clicks on the link and a popup prompt will appear so they can add your site to their favorites list. [code] <script> /* bookmark site */ function bookmarksite(site) { if (document.all) { window.external.AddFavorite("http://www."+ site, site); } else if (window.sidebar && window.sidebar.addPanel) { window.sidebar.addPanel(site,"http://www."+ site,...

How to Display Alexa Traffic Rank Statistics at Your Website

Following codes will display different statistics graphs / charts on your web page. Replace 'yoursite' with your 'domain name' to use these. [code] <!--Daily Traffic Rank Trend--> <script language="JavaScript" type="text/javascript" src="http://xsltcache.alexa.com/traffic_graph/js/g/b/6m?&amp;u=http%3A%2F%2Fwww.yoursite.com+++++"></script> <!--Daily Traffic Rank Trend--> <img src="http://traffic.alexa.com/graph?&amp;w...

Useful Javascript Code

This function will remove any characters or punctuation which is not allowed in input. [sourcecode language="plain"] <script language="javascript"> function test(){ var kwd = document.getElementById("kwd"); kwdv = kwd.value.replace(/[^\d\w\.\?\_\=\&\:\\]/g,''); alert(kwdv); } </script> <input type="text" value="" name="kwd" id="kwd" onblur="test()"> [/sourcecode]