Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Actually using jQuery?
New Posts  All Forums:Forum Nav:

Actually using jQuery? - Page 5

post #41 of 50
Not quite. Get rid of the type check in your if statement. isNaN() always returns a boolean, not a number, so checking its type is pointless. It's the value of the boolean result that you want to test (or rather, the inverse of it), so if !isNaN(test) should be sufficient.

My comment on the use of eval() was for you first usage of it when you were assigning to ifNumber. I think your last usage is "fine", but I'm not sure about the first usage in your latest code.

Note that if you really want to you can use jQuery.isNumeric() instead of the parsing and isNaN() check.
Edited by randomizer - 3/28/14 at 4:29pm
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #42 of 50
Thread Starter 
Huh. So apparently
Code:
eval(blah)

and
Code:
window[blah]

do the same thing. Is this a safer alternative or is it still somewhat risky like eval()? Anyway, I've got this now:
Code:
$(document).ready(function(){
        $("button[name=search]").click(function(){
                if (!isNaN(parseFloat($("input[name=element]").val())) === true) {
                        var ifNumber = $("input[name=element]").val() - 1;
                        var functionInput = window[elementList[ifNumber]];
                        displayInfo(functionInput);
                } else {
                        var ifString = $("input[name=element]").val().toLowerCase();
                        displayInfo(window[ifString]);  
                }

Yeah, I know, the function isn't closed, but there's an identical one for a second column right below it. Thank you everybody, you have been AWESOME! heart.gif
Triumvirate
(20 items)
 
Osmium
(8 items)
 
For Sale: [FS] Z97 system: Xeon and RAM
$160 (USD) or best offer
CPUMotherboardGraphicsGraphics
i7-5775C ASUS Sabertooth Z97 Mark 2 Sapphire RX 480 (reference) MSI Low-Profile 750Ti 
RAMHard DriveHard DriveHard Drive
Corsair Dominator Platinum - 2x8GB Crucial M500 - 960GB Samsung 840 - 250GB WD Scorpio Blue - 1TB 
Hard DriveCoolingCoolingOS
ADATA SP900 - 64GB Scythe BIG Shuriken 2 Rev. B PNY Quadro 600 blower Windows 10 
MonitorMonitorMonitorKeyboard
ASUS VE247H - 1080p Gateway FPD1960 - 1280x1024 Samsung S20D300 - 900p, portrait Rosewill RK9000I - Cherry MX Blue 
PowerCaseMouseOther
Rosewill HIVE 650W Riotoro CR1080 SteelSeries Rival 100 CyberPower 1500PFCLCD 
CPUMotherboardRAMHard Drive
AMD A10-7870K ASRock FM2A88X-ITX+ G.Skill Ripjaws X - 2x4GB @ 2400MT/s Samsung 840 120GB 
CoolingOSPowerCase
Evercool EC-HPS-810CP Windows 7 Pro 64-bit PicoPSU-120-WI-25 Mini-Box M350 
  hide details  
Reply
Triumvirate
(20 items)
 
Osmium
(8 items)
 
For Sale: [FS] Z97 system: Xeon and RAM
$160 (USD) or best offer
CPUMotherboardGraphicsGraphics
i7-5775C ASUS Sabertooth Z97 Mark 2 Sapphire RX 480 (reference) MSI Low-Profile 750Ti 
RAMHard DriveHard DriveHard Drive
Corsair Dominator Platinum - 2x8GB Crucial M500 - 960GB Samsung 840 - 250GB WD Scorpio Blue - 1TB 
Hard DriveCoolingCoolingOS
ADATA SP900 - 64GB Scythe BIG Shuriken 2 Rev. B PNY Quadro 600 blower Windows 10 
MonitorMonitorMonitorKeyboard
ASUS VE247H - 1080p Gateway FPD1960 - 1280x1024 Samsung S20D300 - 900p, portrait Rosewill RK9000I - Cherry MX Blue 
PowerCaseMouseOther
Rosewill HIVE 650W Riotoro CR1080 SteelSeries Rival 100 CyberPower 1500PFCLCD 
CPUMotherboardRAMHard Drive
AMD A10-7870K ASRock FM2A88X-ITX+ G.Skill Ripjaws X - 2x4GB @ 2400MT/s Samsung 840 120GB 
CoolingOSPowerCase
Evercool EC-HPS-810CP Windows 7 Pro 64-bit PicoPSU-120-WI-25 Mini-Box M350 
  hide details  
Reply
post #43 of 50
They don't do the same thing. eval() passes a string through the JS interpreter and attempts to evaluate it (hence the name). It can evaluate arbitrary JS expressions and statements. Using square brackets on the window object with a string between them will do the following:

  1. Look for a property on the window object with that name. You could do this, for example: window["isNaN"]("foobar"). The value of the "isNaN" property is a function that you've already used. They can also be accessed with dot notation, eg. window.isNaN. Since the window object is also the global scope you can access its properties from anywhere without specifying the object name, which is why isNaN works on its own.
  2. If no property exists then it will look for an element with an id or name matching that string, returning the first match it finds by traversing the DOM tree. I don't know why they decided to add this behaviour, I think it's odd.

It just so happens that you are (probably... if I understand what is going on here) looking for element IDs, so that's why it's working. It's safer because you've removed the ability to execute arbitrary code, but document.getElementById() and document.getElementsByName() are better options because it's more obvious what you're trying to do.
Edited by randomizer - 4/9/14 at 3:51am
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #44 of 50
Ohh , also the whole thing with the eval that you mentioned was using a string as an argument, but in this recent post, you are referencing a location in an array or a variable itself.

The explanation above stands. Eval basically runs the given command, and window[] which allows you to reference an object.

In your example from above, doing window["myObject"] is safer than eval because eval will do anything you ask it to, but window just hands you the object which is all you wanted anyways.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #45 of 50
post #46 of 50
Generally speaking, most uses of eval can (and should) be replaced by something else.
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #47 of 50
Normally in forums we get some people who play devils advocate or just generally disagrees with the normal consensus... but in this case... EVERYONE says avoid eval at all costs. thumb.gif So CynicalUnicorn , while this may be some test code or something that no one will ever see, might as well get into the habit of never using eval now =)
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #48 of 50
RCwnemB.jpg
post #49 of 50
Thread Starter 
Code:
function tableSearch(atom) {
        $(document).ready(function(){
                $("area").click(function(){
                        var selector = $('input:radio[name="tableSelect"]:checked').val();
                        switch(selector){
                                case "rb1": 
                                        displayInfo1(atom);
                                        break;
                                case "rb2":
                                        displayInfo2(atom);
                                        break;
                                case "rb3":
                                        displayInfo3(atom);
                                        break;
                                case "rb4":
                                        displayInfo4(atom);
                                        break;
                                default:
                                        break;
                        };
                });
        });
};

I need help. Again. JQuery is being weird. The switch statement affects absolutely nothing; I checked. What happens is when I click on an area in an image, it has an href property that runs this function. On Click 1 when selecting an area with an href running Argument A, nothing happens. On Click 2 when selecting an area with an href running Argument B, it runs the script with Argument A instead. On Click 3, you expect Argument C and get Argument B, and so on. Basically, the code itself is lagging and I have absolutely no idea why. The only thing I can possibly think of is that it has to run the displayInfo function, but it works fine with the search bar function. I am so confused right now.
Triumvirate
(20 items)
 
Osmium
(8 items)
 
For Sale: [FS] Z97 system: Xeon and RAM
$160 (USD) or best offer
CPUMotherboardGraphicsGraphics
i7-5775C ASUS Sabertooth Z97 Mark 2 Sapphire RX 480 (reference) MSI Low-Profile 750Ti 
RAMHard DriveHard DriveHard Drive
Corsair Dominator Platinum - 2x8GB Crucial M500 - 960GB Samsung 840 - 250GB WD Scorpio Blue - 1TB 
Hard DriveCoolingCoolingOS
ADATA SP900 - 64GB Scythe BIG Shuriken 2 Rev. B PNY Quadro 600 blower Windows 10 
MonitorMonitorMonitorKeyboard
ASUS VE247H - 1080p Gateway FPD1960 - 1280x1024 Samsung S20D300 - 900p, portrait Rosewill RK9000I - Cherry MX Blue 
PowerCaseMouseOther
Rosewill HIVE 650W Riotoro CR1080 SteelSeries Rival 100 CyberPower 1500PFCLCD 
CPUMotherboardRAMHard Drive
AMD A10-7870K ASRock FM2A88X-ITX+ G.Skill Ripjaws X - 2x4GB @ 2400MT/s Samsung 840 120GB 
CoolingOSPowerCase
Evercool EC-HPS-810CP Windows 7 Pro 64-bit PicoPSU-120-WI-25 Mini-Box M350 
  hide details  
Reply
Triumvirate
(20 items)
 
Osmium
(8 items)
 
For Sale: [FS] Z97 system: Xeon and RAM
$160 (USD) or best offer
CPUMotherboardGraphicsGraphics
i7-5775C ASUS Sabertooth Z97 Mark 2 Sapphire RX 480 (reference) MSI Low-Profile 750Ti 
RAMHard DriveHard DriveHard Drive
Corsair Dominator Platinum - 2x8GB Crucial M500 - 960GB Samsung 840 - 250GB WD Scorpio Blue - 1TB 
Hard DriveCoolingCoolingOS
ADATA SP900 - 64GB Scythe BIG Shuriken 2 Rev. B PNY Quadro 600 blower Windows 10 
MonitorMonitorMonitorKeyboard
ASUS VE247H - 1080p Gateway FPD1960 - 1280x1024 Samsung S20D300 - 900p, portrait Rosewill RK9000I - Cherry MX Blue 
PowerCaseMouseOther
Rosewill HIVE 650W Riotoro CR1080 SteelSeries Rival 100 CyberPower 1500PFCLCD 
CPUMotherboardRAMHard Drive
AMD A10-7870K ASRock FM2A88X-ITX+ G.Skill Ripjaws X - 2x4GB @ 2400MT/s Samsung 840 120GB 
CoolingOSPowerCase
Evercool EC-HPS-810CP Windows 7 Pro 64-bit PicoPSU-120-WI-25 Mini-Box M350 
  hide details  
Reply
post #50 of 50
Can you provide a link to all of the code (HTML and JS) so we can see what you're trying to do in context? The code you posted and the behaviour you're describing seem nearly unrelated.
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Actually using jQuery?