Click here to Skip to main content
Click here to Skip to main content

A New Discovery?: Omitting File Extension in SRC attribute of IMG tag

, 18 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Image can also be shown without specifying its extension in HTML doc if it is on server
[First of all, I don't know whether I have selected a proper section for this or not. I didn't find a perfect section for this. If the selected section for this trick is not valid, I am sorry; and if possible, I would change it if you have a good section suggestion.]
 
I accidently found a new thing that the extension (including the dot) of an image file can be omitted in the SRC attribute of IMG tag in HTML file.

So
<img src="pencil" />
is valid and showing the image if the actual file name is "pencil.jpg".
 
When I put 3 images pencil.png, pencil.jpg and pencil.gif in a folder and put 3 IMG tags in HTML file.
  <img src="pencil" />
  <img src="pencil" />
  <img src="pencil" />
-----
Update:
This code is showing 3 PNG images in Firefox and Internet Explorer; but Chrome, Opera and Safari are showing 3 JPEG images!
-----
Omitting file extension also worked when I used stylesheet:
<div style="width:100px; height:100px; background-image:url(pencil)"></div>
It is displaying the image as background in a 100x100 pixel sized Div.
 
This trick worked on my computer on WAMP server (Apache) (http://localhost/example.htm), but it didn't work when I opened the file directly in browser (file:///c:/wamp/www/example.htm). This trick also worked with HTML-5 document.

I have Win 7 Ultimate 32-bit with WAMP Server 2.0c installed.
-----
Update:
I checked this on both Windows and Linux servers but it is not working on live servers. Frown | :-(
Totally confused. It is working on my computer having server of Apache and Windows 7 OS but it is not working on live Windows or Linux servers! It is also not working when the file is opened from local harddisk.
-----

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

nikunjbhatt84
Web Developer
India India
No Biography provided
Follow on   Google+

Comments and Discussions

 
QuestionNot Working in Firefox 21.0 Windows 7. PinmemberToywarrior5-Jun-13 20:28 
General[I had first posted this trick in The Launge, then a user to... Pinmembernikunjbhatt8419-Oct-11 2:14 
GeneralReason for my vote of 1 A pointless discovery, just yet anot... PinmemberSteveScanlon19-Oct-11 1:23 
GeneralThe contents of the SRC are meant to be passed verbatim to t... PinmemberRichard Brett18-Oct-11 13:12 
GeneralReason for my vote of 4 Interesting. PinsubeditorWalt Fair, Jr.16-Oct-11 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by nikunjbhatt84
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid