Title: Common Coding Tasks (and how to accomplish them) Post by: Celebrus on May 28, 2009, 12:43:30 am Note: This is by no means a thorough or exhaustive list. As of now it is incomplete. If you would like to see something added to this, please reply to this topic.
Checking whether the user is logged in This function will return true or false depending on whether the user is logged in. Code: function loggedIn() { return $('td.titlebg2 span b').not($("td.titlebg2 span b:contains(News)")).html() ? true : false; } Example: Code: if( ! loggedIn() ) { alert("Hey there, guest"); } Checking whether the user is an admin This function will return true or false depending on whether the user is logged in. Code: function isAdmin() { return ($("td.maintab_back:contains('Admin')").length == 1 || $("td.maintab_active_back:contains('Admin')").length == 1) } Example: Code: if( isAdmin() ) { alert("Hey there, admin"); } Notice that fits in with the forum theme This function will return a string containing HTML that is appropriate for displaying widgets. Code: function widget(m) { return ('<div class="tborder"><div class="windowbg" style="padding:8px">' + m + '</div></div>'); } Example: Code: document.write( widget( 'Hey there!' ) ); Title: Re: Common Coding Tasks (and how to accomplish them) Post by: Agent Moose on May 28, 2009, 06:46:38 am Well, never thought of using the last part :P
Pretty nice man. Title: Re: Common Coding Tasks (and how to accomplish them) Post by: Celebrus on May 28, 2009, 07:12:40 am Thanks ^_^
The main inspiration behind this thread was when I had to search through my entire post history to get the loggedIn function. I thought this would make it easier. Title: Re: Common Coding Tasks (and how to accomplish them) Post by: Agent Moose on May 28, 2009, 07:46:48 am Heh, or you can use this: Code: <script type="text/javascript"> var name = document.getElementsByTagName("span"); for(v=0;v<name.length;v++){ if(name[v].innerHTML.match(/Hello <b>(.*)<\/b>/i)){ var username = RegExp.$1; };}; if(username) alert("Your logged in.") </script> |