Page 1 of 2

Javascript and you

Posted: 01 Sep 2009, 19:53
by RMcGirr83
..well actually me.

My js foo is weak, where can someone go to learn about it and use it. Preferably not in greek which is what most javascript files look like to me as is.

Re: Javascript and you

Posted: 04 Sep 2009, 03:00
by Mr. Bond
I don't remember how I learned JavaScript, or where. Just Internet tutorials I guess. The syntax is very similar to PHP so that makes it easier. Using a good library (*cough* jQuery) can make writing JS code a lot simpler, only needing basic calls to functions to be able to do advanced shtuff (like make AJAX post/requests).

Re: Javascript and you

Posted: 04 Sep 2009, 06:32
by onehundredandtwo
I definitely prefer not to use Javascript, mainly because it's not all standardized (by the W3C) and nearly all of the time CSS can be used for simpler things. Plus I don't really see any reason to make things more complicated, best to stay simple.

I don't really like jQuery because all it does is create effects which are pretty useless and it adds unnecessary bloat.

Re: Javascript and you

Posted: 04 Sep 2009, 06:54
by Obsidian
Personally, I use javascript minimally. I'm not familiar with it, and I find it not to be useful in most circumstances when I code.

Re: Javascript and you

Posted: 04 Sep 2009, 08:52
by RMcGirr83
Mr. Bond wrote:I don't remember how I learned JavaScript, or where. Just Internet tutorials I guess. The syntax is very similar to PHP so that makes it easier. Using a good library (*cough* jQuery) can make writing JS code a lot simpler, only needing basic calls to functions to be able to do advanced shtuff (like make AJAX post/requests).


jQuery is exactly what I am talking about...which I am trying to incorporate into a mod for it's ajax ability. Just can't seem to wrap my head around it.

Re: Javascript and you

Posted: 06 Sep 2009, 14:50
by RMcGirr83
Farken thing is giving me a damn headache. What I believe to be a pretty simple thing is turning out to be a GD nightmare.

All I want to do is refresh at a given interval the "Who's on line" stats (registered users, guests etc).

It is causing me to ingest more beer than I probably should. :(

Re: Javascript and you

Posted: 06 Sep 2009, 15:06
by igorw
It's a little tricky. It will need AJAX.

Re: Javascript and you

Posted: 06 Sep 2009, 16:41
by RMcGirr83
eviL<3 wrote:It's a little tricky. It will need AJAX.


That's what I am trying to do.

Code: Select all

var chat_whois_interval = setInterval(function(){chat_refresh()}, mChatRefresh);  
var chat_refresh = (function(){
      // If not custom page...do nothing
    if(!mChatCustomWhois)
    {
      // Stop
      return;
    }
      // AJAX request
        $jQ.ajax({
          url: mChatFile,
          timeout: 10000,
          type: 'POST',
          async: false,
          data: {mode: 'chatting'},
          dataType: 'html',
       
            success: function(html)
            {
              // If not empty run its part
              if(html != '')
              {         
            // Replace old edited message to new with animation
            $jQ('#mChatWhois').fadeOut('slow', function(){
                // overwrite data to chatting
                $jQ('#mChatWhois').replaceWith(html);
                // Animation ;)
                $jQ('#mChatWhois').css('display', 'none').fadeIn('slow');
            });
           }
          },
          error: function(XMLHttpRequest)
          {
            if(XMLHttpRequest.status == 403)
            {
              // No access alert
              alert(mChatNoAccess);
            }
          },
          beforeSend: function()
          {
            // Refresh stop
            window.clearInterval(chat_whois_interval);
          },
          complete: function()
          {
            // Start refresh
            chat_whois_interval = setInterval(function(){chat_refresh()}, mChatRefresh);
          }       
        });
  });


the sql works as I can get values returned, but the information is not populating my div and I can't figure out why.

Code: Select all

<div id="mChatWhois" style="display:block;">
<p>{MCHAT_TOTAL_USERS_ONLINE}<br />{MCHAT_LOGGED_IN_USER_LIST}</p>
</div>

Re: Javascript and you

Posted: 07 Sep 2009, 12:09
by RMcGirr83
[url=http://www.rmcgirr83.org/mchat.php]If anyone would care to see what happens[/url]

...wait until the page refreshes.

Could really, really, really, (insert infinity symbol here) use a bit 'o help. TIA.

Re: Javascript and you

Posted: 07 Sep 2009, 13:49
by Obsidian
What is the output of the refresh page?

Re: Javascript and you

Posted: 07 Sep 2009, 13:59
by RMcGirr83
[url=http://rmcgirr83.org/mods/before_refresh.jpg]before refresh[/url]

[url=http://rmcgirr83.org/mods/after_refresh.jpg]after refresh[/url]

TIA btw.

Re: Javascript and you

Posted: 07 Sep 2009, 14:08
by Obsidian
Yeah, I saw that. I mean, the code output of the page that is the source of the AJAX refresh.

Re: Javascript and you

Posted: 07 Sep 2009, 14:13
by RMcGirr83
[url=http://rmcgirr83.org/mods/refresh_output.jpg]output[/url]

It's basically the same that is output for the whois online stuff for a forum.

Re: Javascript and you

Posted: 07 Sep 2009, 14:15
by Obsidian
...No, no no no...

Ahh, you know how you are fetching the data from the internal resource, like a page on the forum (to provide the data for the AJAX request)? I need that URL -- I want to see what the actual code put out (like the raw HTML) would be.

Re: Javascript and you

Posted: 07 Sep 2009, 15:42
by RMcGirr83
:oops:

Code: Select all

   // Read function...
   case 'stats':

      // If mChat disabled or user can't view the chat
      if (!$mchat_enable || !$mchat_view || !$mchat_use_custom_page_whois)
      {
         // Forbidden (for jQ AJAX request)
         header('HTTP/1.0 403 Forbidden');
         // Stop running code
         exit('HTTP/1.0 403 Forbidden');
      }
      
      $reading_sql = ' AND s.session_page = "mchat.php"';

      $time = (time() - (intval($config['load_online_time']) * 60));

      // Get number of online guests

      if ($db->sql_layer === 'sqlite')
      {
         $sql = 'SELECT COUNT(session_ip) as num_guests
            FROM (
               SELECT DISTINCT s.session_ip
               FROM ' . SESSIONS_TABLE . ' s
               WHERE s.session_user_id = ' . ANONYMOUS . '
                  AND s.session_time >= ' . ($time - ((int) ($time % 60))) .
               $reading_sql . '
            )';
      }
      else
      {
         $sql = 'SELECT COUNT(DISTINCT s.session_ip) as num_guests
            FROM ' . SESSIONS_TABLE . ' s
            WHERE s.session_user_id = ' . ANONYMOUS . '
               AND s.session_time >= ' . ($time - ((int) ($time % 60))) .
            $reading_sql;
      }
      $result = $db->sql_query($sql);
      $mchat_guests_online = (int) $db->sql_fetchfield('num_guests');
      $db->sql_freeresult($result);
         
      $mchat_online_users = array(
         'online_users'         => array(),
         'hidden_users'         => array(),
         'total_online'         => 0,
         'visible_online'      => 0,
         'hidden_online'         => 0,
         'guests_online'         => 0,
      );

      $mchat_online_users['guests_online'] = $mchat_guests_online;

      $sql = 'SELECT s.session_user_id, s.session_ip, s.session_viewonline
         FROM ' . SESSIONS_TABLE . ' s
         WHERE s.session_time >= ' . ($time - ((int) ($time % 60))) . '
         AND s.session_page = "mchat.php"
         AND s.session_user_id <> ' . ANONYMOUS;
      $result = $db->sql_query($sql);

      while ($row = $db->sql_fetchrow($result))
      {
         // Skip multiple sessions for one user
         if (!isset($mchat_online_users['online_users'][$row['session_user_id']]))
         {
            $mchat_online_users['online_users'][$row['session_user_id']] = (int) $row['session_user_id'];
            if ($row['session_viewonline'])
            {
               $mchat_online_users['visible_online']++;
            }
            else
            {
               $mchat_online_users['hidden_users'][$row['session_user_id']] = (int) $row['session_user_id'];
               $mchat_online_users['hidden_online']++;
            }
         }
      }
      $mchat_online_users['total_online'] = $mchat_online_users['guests_online'] + $mchat_online_users['visible_online'] + $mchat_online_users['hidden_online'];
      $db->sql_freeresult($result);

      $mchat_user_online_link = $mchat_online_userlist = '';

      if (sizeof($mchat_online_users['online_users']))
      {
         $sql = 'SELECT username, username_clean, user_id, user_type, user_allow_viewonline, user_colour
               FROM ' . USERS_TABLE . '
               WHERE ' . $db->sql_in_set('user_id', $mchat_online_users['online_users']) . '
               ORDER BY username_clean ASC';
         $result = $db->sql_query($sql);

         while ($row = $db->sql_fetchrow($result))
         {
            // User is logged in and therefore not a guest
            if ($row['user_id'] != ANONYMOUS)
            {
               if (isset($mchat_online_users['hidden_users'][$row['user_id']]))
               {
                  $row['username'] = '<em>' . $row['username'] . '</em>';
               }

               if (!isset($mchat_online_users['hidden_users'][$row['user_id']]) || $auth->acl_get('u_viewonline'))
               {
                  $mchat_user_online_link = get_username_string(($row['user_type'] <> USER_IGNORE) ? 'full' : 'no_profile', $row['user_id'], $row['username'], $row['user_colour']);
                  $mchat_online_userlist .= ($mchat_online_userlist != '') ? ', ' . $mchat_user_online_link : $mchat_user_online_link;
               }
            }
         }
         $db->sql_freeresult($result);
      }

      if (!$mchat_online_userlist)
      {
         $mchat_online_userlist = $user->lang['NO_ONLINE_USERS'];
      }

      $mchat_online_userlist = $user->lang['REGISTERED_USERS'] . ' ' . $mchat_online_userlist;

      // Build online listing
      $vars_online = array(
         'MCHAT_ONLINE'   => array('total_online', 'l_t_user_s', 0),
         'MCHAT_REG'      => array('visible_online', 'l_r_user_s', 0),
         'MCHAT_HIDDEN'   => array('hidden_online', 'l_h_user_s', 1),
         'MCHAT_GUEST'      => array('guests_online', 'l_g_user_s', 0)
      );

      foreach ($vars_online as $l_prefix => $var_ary)
      {
         if ($var_ary[2])
         {
            $l_suffix = '_AND';
         }
         else
         {
            $l_suffix = '';
         }
         switch ($mchat_online_users[$var_ary[0]])
         {
            case 0:
               ${$var_ary[1]} = $user->lang[$l_prefix . '_USERS_ZERO_TOTAL' . $l_suffix];
            break;

            case 1:
               ${$var_ary[1]} = $user->lang[$l_prefix . '_USER_TOTAL' . $l_suffix];
            break;

            default:
               ${$var_ary[1]} = $user->lang[$l_prefix . '_USERS_TOTAL' . $l_suffix];
            break;
         }
      }
      unset($vars_online);

      $l_online_users = sprintf($l_t_user_s, $mchat_online_users['total_online']);
      $l_online_users .= sprintf($l_r_user_s, $mchat_online_users['visible_online']);
      $l_online_users .= sprintf($l_h_user_s, $mchat_online_users['hidden_online']);

      $l_online_users .= sprintf($l_g_user_s, $mchat_online_users['guests_online']);
      
      $mchat_total_online_users = $mchat_online_users['total_online'];

      $template->assign_vars(array(
         'MCHAT_TOTAL_USERS_ONLINE'      => $l_online_users,
         'MCHAT_LOGGED_IN_USER_LIST'      => $mchat_online_userlist,
      ));      

   break;


:?: