Tuesday, November 10, 2009

Small Javascript Functions


Here is some small JavaScript Functions
(1) Convert text to lower - sometext.toLowerCase();

(2) Convert text to upper - sometext.toUpperCase();

(3) Find Index of - sometext.indexOf('w')

(4) Find Last Index of - sometext.lastIndexOf('a')

(5) Find Character at - sometext.charAt(5)

(6) Find Length - sometext.length

(7) Find Substring - substring(first_index,last_index)

(8) Find Character at - sometext.substr(4,8)

(9) Split the word
var b = 'I am a JavaScript hacker.'
var temp = new Array();
temp = b.split(' ');

(10) onclick="this.select();" to select all text in textbox

(11) oncontextmenu="return false;" to hide context menu

(12) ondragstart="return false;" restrict user to drag

(13) ondrop="return false;" restrict user to drop dragged content

(14) white-space: -moz-pre-wrap; to wrap text for mozilla

(15) word-wrap:break-word; to wrap text for IE

(16) document.getElementById('Text34').removeAttribute("readonly","") removes the attribute of control named Text34

(17) document.getElementById('Text34').setAttribute("readonly","readonly"); sets the attribute value for control named Text34

(18) window.showModalDialog('http://www.google.com', window ,'dialogHeight:50px;;dialogWidth:800px;dialogLeft:10;dialogTop:250')  show dialog box in ID

(19) a=a?a:window.event?window.event:""; //a is event passed in function and viewed both in IE and Mozilla.

(20) "disabled" will disabled the control

(21) readonly="readonly" for the textbox

(22) window.status will show the status we have applied --> onmouseover="window.status='hello';return true"

(23) event.screenX shows the screen X position

(24) event.screenY shows the screen Y position

(25) check which browser viewing navigator.appName == "Microsoft Internet Explorer",navigator.appName=="Netscape"

(26) alert('test') this will show a dialog box showing the message

(27) to use Ceil function  - Math.Ceil(value) 'where value is value to be ceiled

(28) to find the dropdownlist and its selected value as well as test
    var comp=document.getElementById('dropdownlistid')
        comp.options[comp.selectedIndex].text; 'finds the selected text value of the dropdownlist
    comp.options[comp.selectedIndex].value; 'finds the value of the dropdownlist selected item

(29) replace text
    text="java";
    text = text.replace(/java/, "not java");
    -- REMOVING spaces from text
    text=" a s  d f ";
    text = text.replace(/\s+/g,'');
    alert(text);

(30) change the page title
document.title = "This is the new page title.";
What if the page is inside a frameset? In that case just add parent to the front.
parent.document.title = "This is the new page title.";

No comments:

Post a Comment