Archive

Archive for the ‘Sniplets’ Category

getElementsByClassName

Juli 14th, 2009 No comments

Manchmal vermisse ich die Möglichkeit alle CSS-Elemente erfassen zu können, denen der gleiche Klassenname zugewiesen wurde.

Im SelfHTML Forum wurde ich dann fündig, denn dort suchte jemand genau dazu ebenfalls eine Lösung.

Es wird dabei über alle Elemente eines Dokuments hinweg eine Abfrage der Eigenschaft className durchgeführt. Beim IE erreicht man das durch die Referenzen document.all auf alle Elemente, wohingegen Netscape 6+ / Mozilla und Opera7 das mit Hilfe von document.getElementsByTagName(“*”) ermöglichen.

Beim Aufruf wird der Funktion ein Klassenname übergeben, anhand dessen ein Objekt-Array (ret_obj) erstellt und zurückgegeben wird.


function getElementsByClassName(class_name)
{
	var all_obj,ret_obj=new Array(),j=0,teststr;
	if(document.all)all_obj=document.all;
	else if(document.getElementsByTagName && !document.all)all_obj=document.getElementsByTagName("*");
	for(i=0;i<all_obj.length;i++)
	{
		if(all_obj[i].className.indexOf(class_name)!=-1)
		{
			teststr=","+all_obj[i].className.split(" ").join(",")+",";
			if(teststr.indexOf(","+class_name+",")!=-1)
			{
				ret_obj[j]=all_obj[i];
				j++;
			}
		}
	}
	return ret_obj;
}

Meiner Meinung nach eine tolle Alternative, wenn man per Javascript eine ganze Klasse umformatieren möchte.

What happened to Alert and Confirm?

April 29th, 2009 1 comment

We’ve all been on Web sites and received the “Invalid input, please try again” alert and the “Delete record. Are you sure?” confirmation dialog. Developers often want to pop up informational messages like these.

Sidebar, however, has disabled these JavaScript functions. Using popup dialogs goes against the Windows Vista UX guidelines for Sidebar gadgets. If you still feel compelled to use popups, you can emulate these functions.

To do this, first insert a simple one line tag into the head element of your HTML:

<script src="alert.vbs" type="text/vbscript"></script>

Then create a file that contains the following code and name it alert.vbs. Now you can continue to use alert and confirm as you wish.

‘simulate JavaScript alert() function
sub alert(prompt)
      MsgBox prompt, 48 , "Sidebar Gadget"
end sub  

‘simulate JavaScript confirm() function
function confirm(prompt)
      dim res
      res = MsgBox (prompt, 33, "Sidebar Gadget")
      if res=1 then
          confirm = true
      else
          confirm = false
      end if
end function


via Gadgets: Build Your Own Windows Vista Sidebar Gadget.