473,408 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Using window.setTimeout with dynamic parameter


HELP!

Its taken me ages - I'm a newbie and I've compiled bits of code that I've
read in this newsgroup over time to create one of my most intricate
functions to date...

Basically, the script gets called with a single arguement (full path to an
image). The image is supposed to be downloaded to the cache, and when
complete, a new window opened that is slightly larger insize then the images
dimensions... The new window will contain the image, and the new window will
be in the middle of the screen...

Nice?

Well... It almost works... in Mozilla, after a handful of refreshes, the
script works as above... In IE6 though, nothing... no errors... no window..
nothing... During development, I used the Javascript Console to help me and
I believe the problem is where I use the window.setTimeout function.
Specifically, when I try to call the parent function and pass it the image
path, and the use (or overuse?) of quotes...

Can someone help straighten it out as I am proud of it and I think somebody
out there (other than myself) will actually get some use out of this once
its fully working...

The script is below... along with my HTML <img onClick> tag (with newlines
to make it more readable)....

<script type='text/javascript'>
function imageInNewWindow(imgsrc)
{ // Display the image in a new window that is centered in the middle of the
screen
var oImg = new Image();
oImg.src = imgsrc;

var newWinWidth=(oImg.width*1.09);
var newWinHeight=(oImg.height*1.09);
var winX = (screen.width-oImg.width)/2;
var winY = (screen.height-oImg.height)/2;
if (oImg.complete) {
window.open(imgsrc,"myWindowName");
// I remarked the following two lines in case it might be part of the
problem - I think its fine though...

//,"height="+newWinHeight+",width="+newWinWidth+",to p="+winY+",left="+winX+"
");
//,menubar=no,resizeable=no,toolbar=0,location=0,scr ollbars=0");
return true;
}
window.setTimeout("imageInNewWindow('"+imgsrc+"')" , 1000);
return true;
}
</script>

<img src=http://www.myDomain.com/tKnitting-35.jpg
border=0
onClick="imageInNewWindow('http://www.myDomain.com/tKnitting-35.jpg');
return false">

--
Replies please... via the newsgroup, so everyone can learn...
Thanks,
Randell D.
Jul 20 '05 #1
2 5332
"Randell D." <re**********************@and.share.com> wrote in message
news:_LJMb.76063$ts4.40466@pd7tw3no...
<snip>
Basically, the script gets called with a single arguement
(full path to an image). The image is supposed to be downloaded
to the cache, and when complete,
a new window opened that is slightly larger insize then the
images dimensions...
Assuming that you know how big the image is, and that the browser can
(or is willing to) open new windows.
The new window will contain the image,
and the new window will be in the middle of the screen...
The centre of the screen is not always the best place to put a new
window (assuming it can be opened in the first place). On multi-monitor
set ups that might be across a screen boundary or on a completely
different screen to the browser (which might not be where the user is
looking). Then there are MDI inter4face browsers, like Opera, where
positioning based on screen dimensions is meaningless and will often
result in a window that if partly, or fully, off the MDI main window (so
obscured or out of sight).
Nice?
No, the reliability (particularly in the face of pop-up blocking
mechanisms) and the chances that the sizing and positioning attempts
will backfire suggest that an alternative approach might be more
productive.

Last time this script (it is quite a popular concept for a script) was
proposed I knocked together a demonstration of an alternative approach
using DHTML:-

<URL:
http://www.litotes.demon.co.uk/examp...ndowPopUp.html >

<URL: http://www.litotes.demon.co.uk/examp...age_popup.html >
Well... It almost works... in Mozilla, after a handful of refreshes,
the script works as above...
That is a definition of "works"?
In IE6 though, nothing... no errors... no window.. nothing...
During development, I used the Javascript Console to help me and
I believe the problem is where I use the window.setTimeout function.
Specifically, when I try to call the parent function and pass it
the image path, and the use (or overuse?) of quotes...

Can someone help straighten it out as I am proud of it and I think
somebody out there (other than myself) will actually get some use
out of this once its fully working...

The script is below... along with my HTML <img onClick> tag
(with newlines to make it more readable)....

<script type='text/javascript'>
function imageInNewWindow(imgsrc)
{ // Display the image in a new window that is centered in the
//middle of the screen
var oImg = new Image();
oImg.src = imgsrc;

var newWinWidth=(oImg.width*1.09);
There is little chance that the Image object will have the width
property set to a value relating to the size of the image in the image
file at this point. Some browsers never set the width/height of the
image from the dimensions in the image file even when it is loaded (and
others have dummy Image constructors and will not even load the image as
a result of this code).
var newWinHeight=(oImg.height*1.09);
var winX = (screen.width-oImg.width)/2;
var winY = (screen.height-oImg.height)/2;
If you insist on positioning based on screen dimensions then use the
availWidth and availHeight properties of the screen object instead as
they (sometimes) take taskbars and the like into account.
if (oImg.complete) {
The complete property is not a reliable indicator that an Image has
finished downloading . Your IE problem is probably due to this property
being true prior to the Image object knowing the size of the image (IE
does read the image size from the file, one it arrives). Using the
image's onload event is more reliable (but not totally).
window.open(imgsrc,"myWindowName");
// I remarked the following two lines in case it might be
// part of the problem - I think its fine though...

//,"height="+newWinHeight+",width="+newWinWidth+",to p="+winY+
//",left="+winX+"
");
Was that quote closing parenthesis and semicolon intended to be at the
end of the preceding line? (You control the margins of your newsreader
and the insertion of whitespace (line feed/carriage return pairs) into
you code, there is no reason to present your code broken across lines in
a way that renders it erroneous).
//,menubar=no,resizeable=no,toolbar=0,location=0,scr ollbars=0");
In order to justify setting the resizable feature of a pop-up window to
no you need to be certain that you have got the size right on every
browser that will open the pop-up (Your script does not even come close
to doing that). As the sizing controls don't significantly effect the
appearance of a window there isn't a good reason for ever rendering a
window non-resizable, if you get the size right the user will not
attempt to change it and if you get it wrong they will be annoyed that
they can't.

Also, (all else being equal) once you set any of the features for a new
window (such as the size) all remaining window features default to off
so there is only a need to mention features in the list that you want
on.
return true;
}
window.setTimeout("imageInNewWindow('"+imgsrc+"')" , 1000);
If the following image URL is representative the quoting in this
setTimeout looks correct.

<snip> onClick="imageInNewWindow('http://www.myDomain.com/tKnitting-35.jpg'); return false">


Richard.
Jul 20 '05 #2

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bu*******************@news.demon.co.uk...
"Randell D." <re**********************@and.share.com> wrote in message
news:_LJMb.76063$ts4.40466@pd7tw3no...
<snip>
Basically, the script gets called with a single arguement
(full path to an image). The image is supposed to be downloaded
to the cache, and when complete,
a new window opened that is slightly larger insize then the
images dimensions...


Assuming that you know how big the image is, and that the browser can
(or is willing to) open new windows.
The new window will contain the image,
and the new window will be in the middle of the screen...


The centre of the screen is not always the best place to put a new
window (assuming it can be opened in the first place). On multi-monitor
set ups that might be across a screen boundary or on a completely
different screen to the browser (which might not be where the user is
looking). Then there are MDI inter4face browsers, like Opera, where
positioning based on screen dimensions is meaningless and will often
result in a window that if partly, or fully, off the MDI main window (so
obscured or out of sight).
Nice?


No, the reliability (particularly in the face of pop-up blocking
mechanisms) and the chances that the sizing and positioning attempts
will backfire suggest that an alternative approach might be more
productive.

Last time this script (it is quite a popular concept for a script) was
proposed I knocked together a demonstration of an alternative approach
using DHTML:-

<URL:
http://www.litotes.demon.co.uk/examp...ndowPopUp.html >

<URL: http://www.litotes.demon.co.uk/examp...age_popup.html >
Well... It almost works... in Mozilla, after a handful of refreshes,
the script works as above...


That is a definition of "works"?
In IE6 though, nothing... no errors... no window.. nothing...
During development, I used the Javascript Console to help me and
I believe the problem is where I use the window.setTimeout function.
Specifically, when I try to call the parent function and pass it
the image path, and the use (or overuse?) of quotes...

Can someone help straighten it out as I am proud of it and I think
somebody out there (other than myself) will actually get some use
out of this once its fully working...

The script is below... along with my HTML <img onClick> tag
(with newlines to make it more readable)....

<script type='text/javascript'>
function imageInNewWindow(imgsrc)
{ // Display the image in a new window that is centered in the
//middle of the screen
var oImg = new Image();
oImg.src = imgsrc;

var newWinWidth=(oImg.width*1.09);


There is little chance that the Image object will have the width
property set to a value relating to the size of the image in the image
file at this point. Some browsers never set the width/height of the
image from the dimensions in the image file even when it is loaded (and
others have dummy Image constructors and will not even load the image as
a result of this code).
var newWinHeight=(oImg.height*1.09);
var winX = (screen.width-oImg.width)/2;
var winY = (screen.height-oImg.height)/2;


If you insist on positioning based on screen dimensions then use the
availWidth and availHeight properties of the screen object instead as
they (sometimes) take taskbars and the like into account.
if (oImg.complete) {


The complete property is not a reliable indicator that an Image has
finished downloading . Your IE problem is probably due to this property
being true prior to the Image object knowing the size of the image (IE
does read the image size from the file, one it arrives). Using the
image's onload event is more reliable (but not totally).
window.open(imgsrc,"myWindowName");
// I remarked the following two lines in case it might be
// part of the problem - I think its fine though...

//,"height="+newWinHeight+",width="+newWinWidth+",to p="+winY+
//",left="+winX+"
");


Was that quote closing parenthesis and semicolon intended to be at the
end of the preceding line? (You control the margins of your newsreader
and the insertion of whitespace (line feed/carriage return pairs) into
you code, there is no reason to present your code broken across lines in
a way that renders it erroneous).
//,menubar=no,resizeable=no,toolbar=0,location=0,scr ollbars=0");


In order to justify setting the resizable feature of a pop-up window to
no you need to be certain that you have got the size right on every
browser that will open the pop-up (Your script does not even come close
to doing that). As the sizing controls don't significantly effect the
appearance of a window there isn't a good reason for ever rendering a
window non-resizable, if you get the size right the user will not
attempt to change it and if you get it wrong they will be annoyed that
they can't.

Also, (all else being equal) once you set any of the features for a new
window (such as the size) all remaining window features default to off
so there is only a need to mention features in the list that you want
on.
return true;
}
window.setTimeout("imageInNewWindow('"+imgsrc+"')" , 1000);


If the following image URL is representative the quoting in this
setTimeout looks correct.

<snip>

onClick="imageInNewWindow('http://www.myDomain.com/tKnitting-35.jpg');
return false">


Richard.

Thanks for the help.... I think... I know your criticism is constructive...
but you've managed to burst my bubble... 8( I was really pleased with what I
had accomplished...

You've given alot of food for thought there... I'll return to the drawing
board...

Cheers
Randell D.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Michael Winter <M.Winter@blueyonder.co.invalid> wrote in message news:<opr3wd8yvj5vklcq@news-text.blueyonder.co.uk>... > On 24 Feb 2004 14:13:47 -0800, John <johnmark@fastermail.com> wrote: > > >...
3
by: John Smith | last post by:
I would like to close a child window I opened from the parent using setTimout. I have the function below, but it always returns the error "sendWindow is undefined". I would appreciate any...
13
by: MPH | last post by:
Problem: using a named window as target, a new window is opened anyway. Also, both windows have the same name. This happens on SOME machines only env: XP sp2 ie6.0.2900.2180 + sp2 ...
24
by: jonathon | last post by:
Hi all, I have a web app with a popup window for entering data. I don't want to access the web every time this window is opened, as most of the app is AJAX. But I can't figure out how to open...
5
by: chad.a.morris | last post by:
Hi everyone, I didn't know how to sum this up very well for the title, but hopefully the person(s) will come along who can help. I'm trying to use the window.event.clientX value for...
2
by: rain_c1 | last post by:
hi, i think this is a little exercise for real experts, but i suffer from real headaches because of it... :-\ i have an object method (method1), that calls setTimeout with an other method...
2
by: Noggon | last post by:
My function, called first() keeps coming back to me in the javascript console as being undefined. Yet I can't think why it isn't loaded. Maybe I'm brain dead, so could use some help! :-) What...
4
by: Eddie | last post by:
I am opening a windows (well, technically a greybox() call GB_show() which shows a nicer window than normal), and want to wait until that window is closed before moving to the next command. ...
2
by: davidson1 | last post by:
Hai friends..for menu to use in my website..i found in one website....pl look below website.... http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm i downloaded 2 files.... ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.