|
-
Jun 13th, 2006, 06:48 PM
#1
Image Height and Width
ok - I'm a complete n00b at this - so bear with me.
i need to make an autosizing popup window. Here's the current javascript i'm using just to do the popup window:
HTML Code:
<script language="Javascript">
function PopupPic(sPicURL) {
window.open(sPicURL, "TEST",
"resizable=1,status=0,HEIGHT=200,WIDTH=200");
}
</script>
As you can see - all I have is the path to the image. I've seen ways of doing window.open("popup.htm?" + sPicURL ..... and then parsing it in the actual popup.htm page and getting the dimensions that way, but I can't actually access a new page like that - for reasons that are too tedious to mention.
So i was wondering if there was a way of calculating the image width and height on this page and then passing those dimensions as i create the popup window?
-
Jun 14th, 2006, 03:22 AM
#2
Re: Image Height and Width
Don't open the image directly.
Open a page to which you pass the url of the image.
In the page, display just that image, and then in javascript
Code:
function resizewindow() {
var intWidth;
var intHeight;
if (window.innerWidth){
intWidth = window.innerWidth;
intHeight = window.innerHeight;
}else{
intWidth = document.body.clientWidth;
intHeight =document.body.clientHeight;
}
intWidth = document.images[0].width - intWidth;
intHeight = document.images[0].height - intHeight;
window.resizeBy(intWidth, intHeight);
}
-
Jun 14th, 2006, 05:57 AM
#3
-
Jun 16th, 2006, 01:33 PM
#4
Re: Image Height and Width
Load the image on the main page itself. Using the javascript above, get its height and width. Store them in variables. When you open the window, you can pass the height and width values to window.open.
-
Jun 17th, 2006, 03:48 AM
#5
Re: Image Height and Width
Don't open a window. It is pretty much never guaranteed success any more, what with popup blockers and so on.
Instead, (and this is something that's also being abused, but nevermind) you should add a "popup" element to the current page. That gives you the benefit of not having to mess around with window properties and manually resizing to the image size.
Code:
function PopupPic(sPicURL)
{
var i = document.createElement('img');
i.setAttribute('src', sPicURL);
i.style.position = 'absolute';
i.style.visibility = 'hidden';
document.body.appendChild(i);
// centre the image
i.style.left = (document.body.offsetWidth / 2 - i.offsetWidth / 2).toString() + 'px';
i.style.top = (document.body.offsetHeight / 2 - i.offsetHeight / 2).toString() + 'px';
// show
i.style.visibility = 'visible';
}
-
Jun 17th, 2006, 09:30 AM
#6
Re: Image Height and Width
Mendhak: Yeah, that's what I've been trying to do. Only problem is that the pictures seem to be being loaded asynchronously so that the popup window has already popped up before the height and width have been assertained. I thought I could get round that be just doing the below - but the window won't popup - (i got the basics of this code from somewhere else)
HTML Code:
<script language="Javascript">
function getWidthAndHeight() {
this.height;
this.width;
//alert(this.height + " "+ this.width);
window.open(sPicURL, "", "resizable=1,status=0",HEIGHT=" + this.height + ",WIDTH=" + this.width);
return true;
}
function loadFailure() {
alert("'" + this.name + "' failed to load.");
return true;
}
function PopupPic(sPicURL) {
iheight = 0;
iwidth = 0;
var myImage = new Image();
myImage.name = sPicURL;
myImage.onload = getWidthAndHeight;
myImage.onerror = loadFailure;
myImage.src = sPicURL;
//window.open(sPicURL, "", "resizable=1,status=0")
}
</script>
pen: That would probably be a better solution - but I've got some queries:
How do I get it to 'close' - so the user can just click on it and it'll either be hidden or destroyed.
Also, in IE (not Fx), a combobox near the centre of the screen appears over the image - is this just a matter of ZOrdering the image? (gee - how obvious is it that I've never done this before )
Is it possible to add a border to the image? I can edit the pictures if need be - i just thought I'd find out if there was a c0d way.
cheers guys
-
Jun 17th, 2006, 09:35 AM
#7
Re: Image Height and Width
Additions additioned.
Code:
function PopupPic(sPicURL)
{
var i = document.createElement('img');
i.setAttribute('src', sPicURL);
i.style.position = 'absolute';
i.style.visibility = 'hidden';
// border (CSS rule)
i.style.border = '2px solid black';
// click to close
if (window.addEventListener)
i.addEventListener('click', doPopupClick, false);
else if (window.attachEvent)
i.attachEvent('click', doPopupClick);
document.body.appendChild(i);
// centre the image
i.style.left = (document.body.offsetWidth / 2 - i.offsetWidth / 2).toString() + 'px';
i.style.top = (document.body.offsetHeight / 2 - i.offsetHeight / 2).toString() + 'px';
// show
i.style.visibility = 'visible';
}
function doPopupClick(e)
{
var obj = e.target || e.srcElement;
obj.parentElement.removeChild(obj);
}
-
Jun 17th, 2006, 09:40 AM
#8
Re: Image Height and Width
Forgot the zorder.
Code:
i.style.zIndex = '999';
-
Jun 17th, 2006, 09:57 AM
#9
-
Jun 17th, 2006, 10:12 AM
#10
Re: Image Height and Width
Hey ho. I rushed that one a bit.
Fix 1:
Code:
// click to close
if (window.addEventListener)
i.addEventListener('click', doPopupClick, false);
else if (window.attachEvent)
i.attachEvent('onclick', doPopupClick);
Fix 2:
Code:
function doPopupClick(e)
{
var obj = e.target || e.srcElement;
obj.parentNode.removeChild(obj);
}
See if those work. I don't know offhand how to fix the combo box problem. I think that may be a bug in IE.
-
Jun 17th, 2006, 11:06 AM
#11
Re: Image Height and Width
those changes work great 
shame about the ZOrder - I've decided to ignore the issue by moving the image to the top-left corner.
I'd like to make the cursor a hand when it's over picture (to signify it's clickable) - is this something that can be set through a property? Or will I have to add mouseover & mouseout events and modify the cursor accordingly?
-
Jun 17th, 2006, 11:07 AM
#12
Re: Image Height and Width
Code:
i.style.cursor = 'pointer';
-
Jun 17th, 2006, 11:39 AM
#13
Re: Image Height and Width
thanks 
you're right about it being a bug. I've seen it suggested that you can use an iframe (2) - so i figured I could just create an object like you did for the pic and position it but MSDN says i can't create iframes programmatically
Any thoughts?
Edit: btw, I'm afraid I've got to spread it around a bit
-
Jun 17th, 2006, 06:31 PM
#14
Re: Image Height and Width
Dunno. Maybe have a hidden iframe in the code already, and position it above the select box and set it to visible when required. However, I don't know if that will be stretching the limits of your template system
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|