Javascript and you

Discuss the development of future releases of phpBB (phpBB 3.x minor releases) and MODing/Coding related questions.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Javascript and you

Post 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.
User avatar
Mr. Bond
Member
Member
Posts: 89
Joined: 30 Mar 2008, 20:34
Real name: Bobby
Location: 127.0.0.1

Re: Javascript and you

Post 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).
Twitter • Ohloh • GitHub
User avatar
onehundredandtwo
Past Contributor
Past Contributor
Posts: 48
Joined: 23 Jul 2009, 19:47

Re: Javascript and you

Post 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.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: Javascript and you

Post 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.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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. :(
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: Javascript and you

Post by igorw »

It's a little tricky. It will need AJAX.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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>
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: Javascript and you

Post by Obsidian »

What is the output of the refresh page?
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: Javascript and you

Post by Obsidian »

Yeah, I saw that. I mean, the code output of the page that is the source of the AJAX refresh.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: Javascript and you

Post 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.
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Javascript and you

Post 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;


:?:
Post Reply