corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » document.createElement('img')


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author document.createElement('img')
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
28th Aug 11 at 05:35   View Garage View User's Profile U2U Member Reply With Quote

Using this to read image dimensions -

function (fileUrl) {
var img = document.createElement('img');
img.src = fileUrl;
img.onload = function () {
var height = img.height;
var aspect = width / height;
width += 16;
height += 18;
window.open(fileUrl,'_blank','width='+width+', scrollbars=yes, height='+height);
};
}

Few extra checks on the production code to make sure it doesn't make a massive window. Works fine for images but it needs to also work for stuff like PDFs, text files etc, anything that can be displayed inline in the browser. Obviously those other file types can't be loaded in to an img element but rather than doing it anyway and then returning null values for the width and height like a proper programming language would, stupid Javascript just doesn't run at all when you give it non-image URLs.

Is there a way which isn't checking the extension for the presence of the strings jpg, jpeg, gif, tiff etc. which I can test and put some default dimensions in there?

Or other approaches. Just uncomfortable overloading the img element if it's going to cause some browser somewhere to crash massively or give out errors about how shonky that approach is, when in fact it's perfectly acceptable to me.



[Edited on 28-08-2011 by Ian]
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
28th Aug 11 at 09:39   View User's Profile U2U Member Reply With Quote

I'm no expert, but what's wrong with splitting the string?

Won't any other solution involve the server loading the file to find it's type = slow?
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
28th Aug 11 at 09:41   View User's Profile U2U Member Reply With Quote

To be honest, i dont think there is a way of getting around that. I usually use php to detect the dimensions of images byt like you mention, you want to be able to detect it for pdf's and stuff then i dont think you can.

Just on the off chance, have you tried <object> instead of img and see if that works?
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
29th Aug 11 at 23:32   View Garage View User's Profile U2U Member Reply With Quote

Removed the img.onload and just tested the width attribute straight after the src is assigned, and it is now returning zero for the PDF as opposed to not running.

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Anyone clued up on Law? Need advice. mestonian General Chat 11 1007
27th Nov 05 at 18:52
by corsa_munky
 
linking word doc's Haimsey Geek Day 3 575
15th Apr 06 at 20:49
by Haimsey
 
Script to auto activate flash elemtns in web pages... inside Antz Geek Day 4 328
23rd Jan 07 at 00:55
by Antz
 
iFrames topshot_2k Geek Day 5 877
27th Jun 07 at 13:37
by topshot_2k
 
Embeding an Exchange 2010 OWA calander within a frame. Paul Geek Day 2 1070
14th Jun 11 at 08:20
by pow
 

Corsa Sport » Message Board » Off Day » Geek Day » document.createElement('img') 29 database queries in 0.0148120 seconds