JavaScript help...

Benjamin Hall

I AM IRON MAN
Local time
8:42 AM
Messages
301
Location
WI
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 My Computer

Computer Manufacturer/Model Number
HP Pavilion dv4i-2100
OS
Windows 7 Ultimate x86
CPU
Intel Core i3 M350 @ 2.27 GHz
Motherboard
Stock
Memory
3.00 GB
Graphics Card(s)
Intel 1GB HDMI Graphics Card
Sound Card
Stock
Monitor(s) Displays
14.6" HD
Screen Resolution
1200x800 current setting.
Hard Drives
320GB samsung internal
500GB ADATA portable
PSU
Stock
Case
Stock
Cooling
Targus twin-fan chill mat.
Internet Speed
At home, 300kb/sec downloads. At work, 1028kb/sec downloads.
Other Info
Purchased through HP direct, custom build. I highly recommend HP to anyone.
self.focus() should be fine to use for what you want.
 

My Computer My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i7 4790k
Motherboard
MSI Z97S Krait Edition
Memory
8GB Corsair Dominator 1600MHz
Graphics Card(s)
MSI TwinFrozr GeForce GTX770
Sound Card
ASUS Xonar DX/XD 7.1
Monitor(s) Displays
Dell 24" S2409W + Dell 20" E207WFP
Screen Resolution
1920x1080 + 1680x1050
Hard Drives
1x 120GB OCZ Agility 3, 1x 750GB Western Digital Caviar Black, 1x 1TB Western Digital Caviar Blue
PSU
Corsair HX850 modular
Case
Fractal Design Define R4
Cooling
Corsair H60 w/ twin Corsair SP120 fans
Keyboard
Logitech G510S Keyboard
Mouse
Logitech G500S Laser Mouse
Internet Speed
40Mbps
Antivirus
Microsoft Security Essentials
Browser
Google Chrome
Other Info
LG Blu-Ray player
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 My Computer

Computer Manufacturer/Model Number
HP Pavilion dv4i-2100
OS
Windows 7 Ultimate x86
CPU
Intel Core i3 M350 @ 2.27 GHz
Motherboard
Stock
Memory
3.00 GB
Graphics Card(s)
Intel 1GB HDMI Graphics Card
Sound Card
Stock
Monitor(s) Displays
14.6" HD
Screen Resolution
1200x800 current setting.
Hard Drives
320GB samsung internal
500GB ADATA portable
PSU
Stock
Case
Stock
Cooling
Targus twin-fan chill mat.
Internet Speed
At home, 300kb/sec downloads. At work, 1028kb/sec downloads.
Other Info
Purchased through HP direct, custom build. I highly recommend HP to anyone.
What exactly didn't work?
 

My Computer My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i7 4790k
Motherboard
MSI Z97S Krait Edition
Memory
8GB Corsair Dominator 1600MHz
Graphics Card(s)
MSI TwinFrozr GeForce GTX770
Sound Card
ASUS Xonar DX/XD 7.1
Monitor(s) Displays
Dell 24" S2409W + Dell 20" E207WFP
Screen Resolution
1920x1080 + 1680x1050
Hard Drives
1x 120GB OCZ Agility 3, 1x 750GB Western Digital Caviar Black, 1x 1TB Western Digital Caviar Blue
PSU
Corsair HX850 modular
Case
Fractal Design Define R4
Cooling
Corsair H60 w/ twin Corsair SP120 fans
Keyboard
Logitech G510S Keyboard
Mouse
Logitech G500S Laser Mouse
Internet Speed
40Mbps
Antivirus
Microsoft Security Essentials
Browser
Google Chrome
Other Info
LG Blu-Ray player
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 My Computer

Computer Manufacturer/Model Number
HP Pavilion dv4i-2100
OS
Windows 7 Ultimate x86
CPU
Intel Core i3 M350 @ 2.27 GHz
Motherboard
Stock
Memory
3.00 GB
Graphics Card(s)
Intel 1GB HDMI Graphics Card
Sound Card
Stock
Monitor(s) Displays
14.6" HD
Screen Resolution
1200x800 current setting.
Hard Drives
320GB samsung internal
500GB ADATA portable
PSU
Stock
Case
Stock
Cooling
Targus twin-fan chill mat.
Internet Speed
At home, 300kb/sec downloads. At work, 1028kb/sec downloads.
Other Info
Purchased through HP direct, custom build. I highly recommend HP to anyone.
Back
Top