JavaScript help...


  1. Posts : 313
    Windows 7 Ultimate x86
       #1

    JavaScript help...


    So I'm working on adding functions to my battery gadget, and one function I'm trying to add will bring the gadget to the front (make it appear above all other windows) once the battery gets down below 21% (I'll wind up adding a function too that will make it coincide with the user's setting for low battery level) and so I know the main functin will be.

    if
    (percent < 21)

    and then the rest of the function

    but I'm not sure what to use...
    and code monkeys on here?

    I'm not sure if I should use

    onBlur='self.focus()'

    or not...
      My Computer


  2. Posts : 3,322
    Windows 8.1 Pro x64
       #2

    self.focus() should be fine to use for what you want.
      My Computer


  3. Posts : 313
    Windows 7 Ultimate x86
    Thread Starter
       #3

    Okay, cool. I figured it would be, but wanted to be sure.

    So, I tried simply adding it to the "update" function area of my code (the area where it updates the icons based on battery percentage) and it didn't work...

    Here is what I tried (I'll just post the entire "update" function so you can see it in context)

    Code:
    function Update()
    {
      //delcare variables for displaying battery icons.
      var percent = FixedPercent(System.Machine.PowerStatus.batteryPercentRemaining);
      var percentString;
      var timeString;
      var battIcon;
      var onPower = false;
      var charging = false;
      //declare percentage zones for displaying battery icons. 
      //This can be easily edited to fit a new icon set if the set contains more or less icons than what is already here.
      //Simply take 100 and divide it by the total number of battery icons you want to use, then edit the percentages accordingly.
      if (percent > 95)
      {
        battIcon = 17;
      }	
      else if (percent > 89)
      {
        battIcon = 16;
      }
      else if (percent > 83)
      {
        battIcon = 15;
      }
      else if (percent > 77)
      {
        battIcon = 14;
      }
      else if (percent > 71)
      {
        battIcon = 13;
      }
      else if (percent > 65)
      {
        battIcon = 12;
      }
      else if (percent > 59)
      {
        battIcon = 11;
      }
      else if (percent > 53)
      {
        battIcon = 10;
      }
      else if (percent > 47)
      {
        battIcon = 9;
      }
      else if (percent > 41)
      {
        battIcon = 8;
      }
      else if (percent > 35)
      {
        battIcon = 7;
      }
      else if (percent > 29)
      {
        battIcon = 6;
      }
      else if (percent > 23)
      {
        battIcon = 5;
      }
      else if (percent > 17)
      {
        battIcon = 4;
      }
      else if (percent > 11)
      {
        battIcon = 3;
      }
      else if (percent > 5)
      {
        battIcon = 2;
      }
      else
      {
        battIcon = 1;
      }
    
    if (percent < 20)
    {
      self.focus();
    }
      
      percentString = percent + "%";
      if (System.Machine.PowerStatus.isBatteryCharging)
      {
        timeString = " ";
        charging = true;
      }
      else if (System.Machine.PowerStatus.isPowerLineConnected)
      {
        timeString = " ";
        onPower = true;
      }
      else
      {
        timeString = _timeRemaining.TimeRemainingString;
        if (timeString == "")
        {
          timeString = " ";
        }
      }
      if (_currentBattIcon !=  battIcon)
      {
        _imgBatt.src = "icons/battery_" + battIcon + ".png";
        _currentBattIcon = battIcon;
      }
      if (_currentTimeString != timeString)
      {
        if (_currentTimeString == " ")
        {
          //If transitioning to having a time string present, clear the large percent text and force the percent text to update below.
          _txtPercentageLarge.value = " ";
          _currentPercentString = " ";
        }
        if (timeString == " ")
        {
          //If transitioning from having a time string present, clear the small percent text and force the percent text to update below.
          _txtPercentage.value = " ";
          _currentPercentString = " ";
        }
        _txtTime.value = timeString;
        _currentTimeString = timeString;
      }
      if (_currentPercentString != percentString)
      {
        if (_currentTimeString == " ")
        {
          _txtPercentageLarge.value = percentString;
        }
        else
        {
          _txtPercentage.value = percentString;
        }
        _currentPercentString = percentString;
      }
      if ((_currentOnPower != onPower) || (_currentCharging != charging))
      {
        _currentOnPower = onPower;
        _currentCharging = charging;
        if (charging)
        {
          _imgPower.src = "icons/power_red.png";
        }
        else if (onPower)
        {
          _imgPower.src = "icons/power_black.png";
        }
        else
        {
          _imgPower.src = "icons/power_blank.png";
        }
      }
      setTimeout(Update, 2000);
    }
    But it didn't work. :/ I must be missing something lol.. .maybe I need to declare an entirely new function?
      My Computer


  4. Posts : 3,322
    Windows 8.1 Pro x64
       #4

    What exactly didn't work?
      My Computer


  5. Posts : 313
    Windows 7 Ultimate x86
    Thread Starter
       #5

    Well, I tested it out,

    Code:
    if (percent < 20)
    {
      self.focus();
    }
    by letting my battery drain down below 20 percent, but it didn't bring the app to the front of all my other open widnows :/
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:42.
Find Us