Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search function

Status
Not open for further replies.

Fumac

Programmer
Jan 2, 2001
16
SE
Hi,

I wonder how to use search().
My problem is that I have an URL to a picture, but want to know if the choosen picture is jpeg or gif format. Maby there´s a easier way to do this.
The URL comes from an '<input type=file ...>'.

Thank you for any respons

/Fumac
 

If you have an URL to a picture, you have it's extention also.
Specify exactly what the problem is.

Andrew | starway@mail.com
 
Hello Andrew,
The url comes from an input used for uploading images, and every time someone uploads an image, i want to know it´s extention.
My problem is that i don´t know how to pick out this little piece of information from the string using javascript.

I hope this helps you understand what i mean.

/Fumac

 

OK, now I understand. It's very easy.
Let's say, you have a form element named FIELD1, from which you get filenames.
In order to receive file extentions you have to get last 3 chars of a string:

filename = document.forms[0].field1.value;
ext = filename.substring(filename.length-3, filename.length);

You may also write it as one expressin if you want:

ext = document.forms[0].field1.value.substring(document.forms[0].field1.value.length-3, document.forms[0].field1.value.length)

where
document.forms[0].field1.value - total filename string
document.forms[0].field1.value.length - number of characters in it

we use Substring() function to get only the chars we need. In our case - last three ones.
It works everywhere - IE, NN, Opera.
Enjoy!

Andrew | starway@mail.com

P.S. I just recalled that some files have 4-char extention, with .JPEG being one of them. Maybe you should check it too.
 
Thank you Andrew, I´ll try to solve that problem...(them 4 digits).
It´s much easier now when I know how to work.

Thank you :)

/Fumac
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top