Eye On Asia - Quality Japanese Anime Figures
Cardfight Vanguard
Otaku online web store - anime, manga and more
Genki Gear - Awesome anime-styled T-shirts
Anime, manga and merchandise superstore
Anime League - Anime events and community building
UK Anime Network
|
|
|
|
|
|
|
|
UK Anime Network
Main Site Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Go Back   UK Anime Forums > General > UKA Garage

UKA Garage Need help with hardware? See if our army of boffins can help...

Reply
 
Thread Tools Display Modes
Old 25-04-2012, 05:19 PM   #1
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default Javascript help

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
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!
Necro is offline   Reply With Quote
Old 25-04-2012, 06:09 PM   #2
rjessop
Registered
 
rjessop's Avatar
 
Join Date: Sep 2010
Location: Nottingham
Posts: 790
Default

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]();
}
rjessop is offline   Reply With Quote
Old 25-04-2012, 06:13 PM   #3
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default

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
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!

Last edited by Necro; 25-04-2012 at 08:12 PM.
Necro is offline   Reply With Quote
Old 25-04-2012, 08:53 PM   #4
rjessop
Registered
 
rjessop's Avatar
 
Join Date: Sep 2010
Location: Nottingham
Posts: 790
Default

Pretty surprised that worked
rjessop is offline   Reply With Quote
Old 25-04-2012, 10:26 PM   #5
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default

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.
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!
Necro is offline   Reply With Quote
Old 25-04-2012, 11:48 PM   #6
happosai
=(^;^)=
 
happosai's Avatar
 
Join Date: Jun 2005
Location: Stockport, Manchester
Posts: 2,583
Default

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 + "()");
However, what you're trying to do is generally a bad idea and can usually be accomplished in a far more elegant fashion.

[Happosai]
__________________
. /V\ . o O You're an anime fan?
=(@;@)= . . You live in the North-West of England?
~(,,) . . . You're a member of NanimeW?
happosai is offline   Reply With Quote
Old 26-04-2012, 12:25 AM   #7
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default

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.
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!
Necro is offline   Reply With Quote
Old 01-05-2012, 04:05 PM   #8
donmurray29
Registered
 
donmurray29's Avatar
 
Join Date: Feb 2009
Posts: 96
Default web sites

try creativecow.net, lynda.com, adobe forums, youtube for downloading tutorials, best thing is copying the script you like from the web site
donmurray29 is offline   Reply With Quote
Old 01-05-2012, 06:18 PM   #9
megalomaniac
「遊びは終わりだ」
 
megalomaniac's Avatar
 
Join Date: Mar 2010
Location: Nottingham
Posts: 2,747
Default

How did I miss this? A programming thread is not complete without my input Although happosai pretty much covered it.

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
megalomaniac is offline   Reply With Quote
Old 01-05-2012, 08:41 PM   #10
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default

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
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!
Necro is offline   Reply With Quote
Old 01-05-2012, 09:07 PM   #11
megalomaniac
「遊びは終わりだ」
 
megalomaniac's Avatar
 
Join Date: Mar 2010
Location: Nottingham
Posts: 2,747
Default

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
megalomaniac is offline   Reply With Quote
Old 02-05-2012, 12:12 PM   #12
Necro
Grim User
 
Necro's Avatar
 
Join Date: Jan 2010
Location: Manchester
Posts: 2,763
Default

Awesome, I'll have a look and mess around with that when i'm back from college.
__________________
MyAnimeList | Last.fm
Now with upoutdated account information!
Necro is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:18 AM.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.