1

Wondering how to add in js script a three more !== or statements. The following script was provided as a spectacular answer by jsfriend00 but the problem is I made the mistake of not mentioning there's three more textareas unaccounted for. I'm stuck with the javascript on the remaining or !== conditions. In the following script how to take say !== to each of the following var obj?

//VIEW FILE for private networking stream between admin and clientele:

 <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); ?>

 <html>
 <title>Chat</title>
 <head>

 <?php echo smiley_js(); ?>

 </head>
 <body>

 <?php echo form_open('controller/function');?>
 Reload chat<?php echo form_submit('reload','reload chat');?>
 <?php echo form_close();?>


 <?php if(isset($user_query)): foreach($user_query->result() as $row):?>

 <p>Created at: <?php echo $row->created_at; ?> </p> 

 <p>Updated at: <?php echo $row->updated_at; ?> </p> 

 <p><label for="update"> <?php echo $username_sess.'&nbsp;said'?>:<span class="optional"></span></label>  

  <textarea name="update" id="update" rows="7" cols="30"><?php echo $row->clt_rep_stream ?></textarea>


  <?php echo form_open('controller/function');?>

  <?php echo form_hidden('user_id',$row->user_id) ?>

  <p><label for="adm_update">reply to client:<span class="optional"></span></label>                                
  <textarea name="adm_update" id="adm_update" rows="7" cols="30"> </textarea>
  <?php echo form_error('adm_update'); ?>
   </p>
   <?php echo form_submit('submit','reply');?>

   <?php echo form_close();?>


   <?php echo form_open('controller/function'); ?>

   <?php echo form_hidden('user_id',$row->user_id) ?>

   <p>Replied at: <?php echo $row->adm_created_at; ?> </p> 

   <p>Updated at: <?php echo $row->adm_updated_at; ?> </p> 

   <p><label for="reupdate"> <?php echo $username_adm_sess.'&nbsp;replied:'?><span class="optional"></span></label>  

   <textarea name="reupdate" id="reupdate" rows="7" cols="30"><?php echo $row->adm_rep_stream ?> </textarea>
   <p>                             
   <?php echo form_submit( 'submit', 'update reply'); ?>
   </p>

  <?php echo form_close(); ?>

  <?php endforeach; ?>

  <?php endif; ?>

   <script>
   function autoRefresh(refreshPeriod) {
   var obj = document.getElementById("create"); 
   var obj_two = document.getElementById("update"); 
   var obj_three = document.getElementById("delete"); 
   var obj_four = document.getElementById("reload"); 

   function refreshIfSafe() {
   if (document.activeElement !== obj) { 
    window.location.reload();
  } else {
    setTimeout(refreshIfSafe, refreshPeriod);
   }
 }
    setTimeout(refreshIfSafe, refreshPeriod);
 }

   autoRefresh(2 * 1000);

   </script>

   </body>
   </html>

I tried the following but didn't work:

    if(document.activeElement !==(obj || obj_two))

I tried the following after Brad's post which partially worked but any update or replies after the first two and the following js is rendered null and void which is really confusing:

    if(document.activeElement !== obj && document.activeElement !== obj_two)
eNigma
  • 101
  • 4
  • 10
  • `obj || obj_two` evaluates to `obj` if it is a truthy-value, or `obj_two` otherwise so the conditional `x !== (obj||obj_two)` can be thought of as `x !== obj_OR_obj_two_but_you_dont_know_which`. –  Sep 17 '12 at 00:25

3 Answers3

2

Each condition is separate. Treat it as such.

(document.activeElement !== obj) && (document.activeElement !== obj_two) && ...

You could also do this in a loop if your elements weren't explicitly called out like they are, but I don't know how this would fit in your code, as I don't know what your actual code looks like.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • the answer you provided worked. I tried to check it but got an error message that I couldn't except within 9 minute period. – eNigma Sep 17 '12 at 00:15
  • I like the idea of the loop but not sure at all how that would be done. The script i posted is at the end of my view file. I'm working in codeigniter; MVC. – eNigma Sep 17 '12 at 00:16
  • hey brad I made mistake I can't make the code you posted work unless I do the following: (document.activeElement !== obj && document.activeElement !== obj_two) – eNigma Sep 17 '12 at 01:54
0

Untested, but should work:

<script>
  function autoRefresh(refreshPeriod) {
    var elementArray = [
      document.getElementById("create"),
      document.getElementById("update"),
      document.getElementById("delete"),
      document.getElementById("reload")
    ];

    function refreshIfSafe() {
      if (elementArray.indexOf(document.activeElement) !== -1) {
        window.location.reload();
      } else {
        setTimeout(refreshIfSafe, refreshPeriod);
      }
    }
    setTimeout(refreshIfSafe, refreshPeriod);
  }

  autoRefresh(2 * 1000);
</script>

Or

<script>
  function autoRefresh(refreshPeriod) {
    var elementArray = ["create", "update", "delete", "reload"];    

    function refreshIfSafe() {
      if (document.activeElement && elementArray.indexOf(document.activeElement.id) !== -1) {
        window.location.reload();
      } else {
        setTimeout(refreshIfSafe, refreshPeriod);
      }
    }
    setTimeout(refreshIfSafe, refreshPeriod);
  }

  autoRefresh(2 * 1000);
</script>
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • tried both didn't work. However I tried Brad's solution which worked when I did: (document.activeElement !== obj && document.activeElement !== obj_two). Another problem though this worn't only for the first two within my foreach loop. Going to post more code to show example. – eNigma Sep 17 '12 at 01:57
-2

You can try ... no don't !

if(!(document.activeElement in [obj,obj_two])){
    // ...
}

By the way, when comparing objects, you actually compare their references adress in memory ... so '!=' is enough.

Stphane
  • 3,368
  • 5
  • 32
  • 47
  • 1
    Nooo. Don't say "references". It's not true. `==` and `===` are well-defined in JavaScript *without* talking about "references" and in fact `!=` is orthogonal to object-identity (or "reference" equality). (The only references in JavaScript are *l-values* like property names on the left-side of an assignment operator.) –  Sep 17 '12 at 00:20
  • less verbose and so powerfull but so forgotten ^^ – Stphane Sep 17 '12 at 00:21
  • 2
    Noo, not "addresses in memory" either. Also that won't work correctly, `in` works with **keys**, and *not values* :( `"a" in ["a","b"]` is false, while `"a" in {"a":1,"b":1}` is true. For arrays `Array.indexOf` would work here. It uses `==` for the comparssion, but works sensibly because `==` is defined sensibly for DOM elements (that is, they are compared "by object identity"). –  Sep 17 '12 at 00:31
  • See http://stackoverflow.com/questions/3649321/is-there-a-way-to-check-if-two-dom-elements-are-equal and https://developer.mozilla.org/en-US/docs/DOM/Node.isSameNode for how to get "value equality" with a DOM node check. (`==` and `===` are *always* "object identity" with two DOM elements, the use of `===` simply prohibits type-coercison should one side not be a DOM element) –  Sep 17 '12 at 00:36
  • -1 The [in operator](http://ecma-international.org/ecma-262/5.1/#sec-11.8.7) tests whether an Object has a property with a particular name, it doesn't test values. The expression will almost certainly always return false, and test true. And the part about `address in memory` is junk, `!=` uses the [Abstract Equality Comparison Algorithm](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3), it has nothing to do with memory addresses. – RobG Sep 17 '12 at 03:27
  • Gotcha ... Thank's for reminding me this ! I've never used in key word to compare objects though : I would deserve the cone of shame lol ... I wont delete this post since it is a good "bad exemple" :) – Stphane Sep 17 '12 at 08:09