View Full Version : Javascript Help
hotmail
3rd July 2011, 12:20
Ey,
Last day(s) i m starting to code a bit with javascript, but i keep on getting the same problem. so i hope somebody can help me with this:
where i am looking for : a fuction that returns true when the filename ends with ".set". ( its for a (team) setup database).
my try:
function getfiletype(Name){
var Typestring='';
var Lastdot =0;
for(i=0; i<Name.length;i++){
if(Name[i] =='.'){
Lastdot = i;
}
}
for(i=Lastdot; i<Name.length; i++){
Typestring += Name[i];
}
if(Typestring == '.set'){
return true;
}
else{
return false;
}
}
when i debug my function its working until the if(Typestring == '.set')
he just totally skips that part. (i cant find out why:().
hope somebody can help me with the basics of javascript.:thumbsup:
because its kinda new for me:nod:
DarkTimes
3rd July 2011, 12:31
function getfiletype(name) {
return name.substr(-4) == ".set";
}
Although to actually get the file extension in JavaScript, have a look at this Stack Overflow question (http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript).
hotmail
3rd July 2011, 12:37
function getfiletype(name) {
return name.substr(-4) == ".set";
}
substr(-4)
Although to actually get the file extension in JavaScript, have a look at this Stack Overflow question (http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript).
many thanks looks way more clean then my code.
hotmail
3rd July 2011, 12:44
Ey i hope you dont mind me ask ,but after google-ing for the last couple of days, i see some people using :
return name.substr(-4) == ".set"
and others:
return name.substr(-4) == '.set'
is there a difference in javascript ? between " " or ' ' ?
DarkTimes
3rd July 2011, 12:50
As far as I'm aware it's just a preference thing, it makes no difference. I actually tend to use single-quotes ' ', purely because you don't have to hold shift to type them, I'm not sure why I didn't in my example.
Again Stack Overflow (http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript) has it covered.
hotmail
3rd July 2011, 12:59
okey im getting the hind, i ll use stackoverflow more ^^
DarkTimes
3rd July 2011, 13:05
Hehe, it wasn't a hint, it's just that Stack Overflow is the single most useful programming resource on the internet. :p
Dygear
4th July 2011, 03:04
I second that, StackOverflow is pretty awesome.
MadCatX
4th July 2011, 11:22
Just a little note about DarkTimes' code, which is perfectly fine but it can't handle files whose extension is not 3 chars long (.h .c .torrent .jpeg). I suggest using something like this. (It's not like you need it here, I just thought I should present a "proper" solution in case you need it some day)
function getfiletype(name) {
var typestring = "";
var dot;
for(dot = name.length - 1; dot > -1; dot--) {
if(name.substr(dot, 1) == ".")
break;
}
typestring = name.substr(dot);
return (typestring == ".set");
}
EDIT: Or even better
function getfiletype(name) {
var typestring = name.substr(name.lastIndexOf("."));
return (typestring == ".set");
}
Dygear
4th July 2011, 15:34
function isFileType(fileName, fileType) {
return (fileName.substr(fileName.lastIndexOf('.')) == fileType);
}
function isSetupFile(fileName) {
return isFileType(fileName, '.set');
}
echo ((isSetupFile(file)) ? 'Is' : 'Not') + ' a setup file.';
:bath:
DarkTimes
4th July 2011, 15:36
function getFileExtension(filename) {
return filename.split('.').pop();
}
:p
Dygear
4th July 2011, 15:47
function getFileExtension(filename) {
return filename.split('.').pop();
}
:p
:fence:, you win this round, but there will be others!
function isFileType(fileName, fileType) {
return (fileName.split('.').pop() == fileType);
}
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.