|
|
|
|
|
|
|
|
|
|||||||
| UKA Garage Need help with hardware? See if our army of boffins can help... |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
I'm a complete newbie to javascript, and i'm trying to figure something out. I figured I'd ask here to see if anybody can help, as I'd assume some of you can build websites and stuff
I want to know if it is possible to call a function by using a variable that has the same value as the name of the function. What I am trying to do is make a certain function be executed depending on the date, so if it was April 25th (today), the function "april25()" would run. I'm trying to achieve this by storing the date as a variable and referencing the variable instead of the function name, but I can't figure out how to get it to work, and am now wondering if it's even possible Hope that's coherent |
|
|
|
|
|
#2 |
|
Registered
Join Date: Sep 2010
Location: Nottingham
Posts: 799
|
I haven't written any Javascript in years, but this might work and is totally untested:
Code:
o = new Object();
o.april25 = function () { alert("Today is April 25th"); };
var todaysDate = "april25"; //Presumably you are getting the date somehow
if( todaysDate in o )
{
o[todaysDate]();
}
|
|
|
|
|
|
#3 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
Cheers, I'll try and test it and see if it works.
Edit: on it's own, it works. now to try and mash it into my existing code and try that. Thanks Edit2: I think i've got it working as intended, after being an idiot for longer than I should have been. Thanks again Last edited by Necro; 25-04-2012 at 08:12 PM. |
|
|
|
|
|
#4 |
|
Registered
Join Date: Sep 2010
Location: Nottingham
Posts: 799
|
Pretty surprised that worked
|
|
|
|
|
|
#5 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
haha, it's working dandy
the only issue now is that my work is made infinitely more tedious due to the lack of .txt read support in javascript, but eh, I can work around it. I'm sure professional web developers would commit seppuku upon seeing it though. |
|
|
|
|
|
#6 |
|
=(^;^)=
Join Date: Jun 2005
Location: Stockport, Manchester
Posts: 2,586
|
You could use the eval() function, which takes a string as its argument and executes its contents as code, e.g.
Code:
x = "april25"; eval(x + "()"); [Happosai]
__________________
. /V\ . o O You're an anime fan? =(@;@)= . . You live in the North-West of England? ~(,,) . . . You're a member of NanimeW? |
|
|
|
|
|
#7 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
Yeah, It's a really stupid botch job i've got running. To do what I want properly i'd need to learn a server script like PHP or something, but I really don't understand the syntax or anything, so I'll just do it this way
I mean, because of the text file thing, I've got it displaying javascript files for the data. That surely isn't a good move It's only as a little project to help me learn though, so it's nothing major. |
|
|
|
|
|
#8 |
|
Registered
Join Date: Feb 2009
Posts: 96
|
try creativecow.net, lynda.com, adobe forums, youtube for downloading tutorials, best thing is copying the script you like from the web site
|
|
|
|
|
|
#9 |
|
「遊びは終わりだ」
Join Date: Mar 2010
Location: Nottingham
Posts: 2,770
|
How did I miss this? A programming thread is not complete without my input
If you want to do something useful/cool with your new found JavaScript knowledge get reading up on AJAX & PHP or Ruby on Rails. That's the good shit
__________________
"If there's anything more important than my ego around, I want it caught and shot now!" MyAnimeList | MyFigureCollection | Twitter | Tumblr | Blog |
|
|
|
|
|
#10 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
I was trying to learn some php for the whole file reading job i'd like doing (it would make alot of the Javascript redundant) but I couldn't figure it out for either text files or, a bit more complex, excel spreadsheets. It didn't help I'm really not sure on how to test php script on my desktop pc, as obviously it's not a server, nor do I know how to install it.
bleh |
|
|
|
|
|
#11 |
|
「遊びは終わりだ」
Join Date: Mar 2010
Location: Nottingham
Posts: 2,770
|
You're a Windows man right? If so you'll need IIS running & PHP installed, plenty of tutorials on the web for that. If you need a hand with PHP code drop me a PM or Tweet or something. I've done a lot of PHP over the years, coming from a C/C++ background it makes a great deal of sense to me.
__________________
"If there's anything more important than my ego around, I want it caught and shot now!" MyAnimeList | MyFigureCollection | Twitter | Tumblr | Blog |
|
|
|
|
|
#12 |
|
Grim User
Join Date: Jan 2010
Location: Manchester
Posts: 2,778
|
Awesome, I'll have a look and mess around with that when i'm back from college.
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|