Archive

Archive for the ‘Javascript’ Category

Extended Function getTagFromIdentifierAndTitle to get Fill-In enabled Dropdown

January 26th, 2010 No comments

The function “getTagFromIdentifierAndTitle” is used by SharePoint particularly for changing form fields. But it is good enough only if you have straight forward fields. If you have enabled “Fill-in” values, it will not be able to find the correct element. Here is a extended version of this function to get the “Fill-in” enabled field.

function getTagFromIdentifierAndTitle(tagName, identifier, title)
{
	var len = identifier.length, colonindex, splittitle, taglen, titlelen = title.length, slen;
	var tags = document.getElementsByTagName(tagName);
	for (var i=0; i < tags.length; i++)
	{
		var tempString = tags[i].id;
		taglen = tags[i].title.length;
		if(taglen<titlelen)
			continue;
		splittitle = tags[i].title.replace(title,"");
		slen = splittitle.length;
		if(slen == taglen || (slen > 0 && splittitle.indexOf(":") == -1))
			continue;
		if ((identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
		{
			return tags[i];
		}
	}
	return null;
}
Categories: Javascript, SharePoint Tags:

Crash FreeTextBox using Javascript

December 28th, 2006 2 comments

Would you like to crash FreeTextBox? Here is the trick. Visit the page http://freetextbox.com/demos/Default.aspx

Click on HTML view and copy/paste the following code.

<script language="javascript">alert("Hi");</script>
Hello

Press “Save” button. This will convert your code into

<script language="javascript">
	<![CDATA[lert("Hi");]]>
</script>
Hello

Note that “a” is missing. Pretty interesting!!! hmmmmmm….
Now again click on HTML view and copy/paste following code

<script language="javascript">
<![CDATA[alert("Hi");]]>
</script>
Hello

Now you will see error….. Hurray…… You crashed FreeTextBox !!!!!!!!!!!!!!!!
Now you might think how can I write javascript in FreeTextBox? Here is the solution.

<script language="javascript">
<!--	alert("Hi"); -->
</script>
Hello

Now it will perfectly show you script and will execute without any problem.

Categories: Javascript Tags:

Be Careful When You Are Using Javascript parseInt() Function

January 5th, 2006 6 comments

First of all guess the result and then get the actual answer by clicking on the button “Show Result”

parseInt(“04″)=?;

parseInt(“08″)=?;

In Javascript if the string starts with “0″, the string will be
parsed as if it is octal number. (i.e. base 8) instead of decimal
number(i.e. base 10).

So if you are parsing the string containing integer value and fear about leading zeroes, always specify the base as shown below.

parseInt(“04″,10); (here 10 is the base)
parseInt(“08″,10);

Categories: Javascript Tags:

Javascript : Getting and Setting Caret Position in Textarea

December 27th, 2005 36 comments
function doGetCaretPosition (ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
	ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}
function setCaretPosition(ctrl, pos){
	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

Here “ctrl” is the Textarea object. This script works well with Internet Explorer, Firefox and Opera.

You can check it at Get/Set Caret in Textarea Example

Categories: Javascript Tags: