473,399 Members | 4,177 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,399 software developers and data experts.

JavaScript sometimes working ok and sometimes not

Hello all!

Could you tell me where is the error on the below code, because the script
is sometimes
working correctly and sometimes is not working correctly. I want my new
window with picture
to be fitted/enlarged to original size. The page on the below address:
http://www.kapy.bydg.pl/~marcinz/stolarnia/.
I have Windows XP and IE 6.0. and everything works correctly. If I run the
page on Win 2000, IE 5.0.
it is not good-working.

<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
function funkcja(rysunek)
{

obrazek=new Image()
obrazek.src=rysunek

zmienna_w=obrazek.width+50
zmienna_h=obrazek.height+50

zmienna='toolbar=yes,location=yes,scrollbars=yes,w idth=' + zmienna_w +
',height=' + zmienna_h

return window.open(rysunek,'oknoObr',zmienna)

}

</SCRIPT>
.....
<a href="#"
OnClick="funkcja('WojtStol_files/drzwi/wewnetrzne/100_0114.gif');"><img
src="WojtStol_files/drzwi/wewnetrzne/100_0114.gif" width=150 height=100
border=1></a>
......
Jul 23 '05 #1
2 6683
Kamyk wrote:
Could you tell me where is the error on the below code, because the script
is sometimes working correctly and sometimes is not working correctly.
[...]
<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
Omit the deprecated `language' attribute. And although HTML is not
generally case-sensitive, you should use lowercase characters only
where possible.
function funkcja(rysunek)
{

obrazek=new Image() ^[1] [2]^^^^^ ^[3]

[1] Variables should always be declared, using the `var' keyword.

[2] Host objects like Image should be tested prior to usage:
<http://pointedears.de/scripts/test/whatami>, paragraph 2.

[3] Do not rely on automatic semicolon insertion but end all
statements with a semicolon.
obrazek.src=rysunek

zmienna_w=obrazek.width+50
zmienna_h=obrazek.height+50
Most certainly it does not work because image loading is done asynchronously
by the UA (while the script engine continues interpretation) and so the
`width' and `height' properties do not return proper values prior. You
should use the `onload' event which should fire once the image has been
loaded and so its dimensions can be obtained:
[...]
<a href="#"
OnClick="funkcja('WojtStol_files/drzwi/wewnetrzne/100_0114.gif');"><img
src="WojtStol_files/drzwi/wewnetrzne/100_0114.gif" width=150 height=100
border=1></a>
The above will not work without script support. The below quick hack
should do:

var obrazek, intv, w;

function funkcja(rysunek)
{
if (typeof Image != "undefined")
{
obrazek = new Image()

if (window.setInterval)
{
intv = window.setInterval(
(function()
{
if (obrazek.loaded && w)
{
window.clearInterval(intv);
if (w.innerWidth)
{
w.innerWidth = obrazek.width + 50;
}
else if (window.clientWidth)
{
w.clientWidth = obrazek.width + 50;
}

if (w.innerHeight)
{
w.innerHeight = obrazek.height + 50;
}
else if (w.clientHeight)
{
w.clientHeight = obrazek.height + 50;
}

if (w.focus) w.focus();
}
}).toString(),
100);
}

obrazek.onload = function()
{
this.loaded = true;
}

obrazek.src = rysunek;

return (w = window.open(
rysunek,
'oknoObr',
'toolbar=yes,location=yes,scrollbars=yes'));
}
}

...

<a href="WojtStol_files/drzwi/wewnetrzne/100_0114.png"
onclick="return !funkcja(this.href);"<img src="WojtStol_files/drzwi/wewnetrzne/100_0114_thumbnail.png"

alt="Alternative text -- required!"
width="150" height="100" border="1"></a>

Another alternative which I consider more reliable than your (improved)
approach is generating an entire HTML document to contain the image in
the popup window and use the `onload' handler of either its `body'
element or the `img' element instead, as implemented in enlargeImg():

<http://pointedears.de/scripts/window.js>
PointedEars
Jul 23 '05 #2
Kamyk wrote:
Could you tell me where is the error on the below code, because the script
is sometimes working correctly and sometimes is not working correctly.
[...]
<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
Omit the deprecated `language' attribute. And although HTML is not
generally case-sensitive, you should use lowercase characters only
where possible.
function funkcja(rysunek)
{

obrazek=new Image() ^[1] [2]^^^^^ ^[3]

[1] Variables should always be declared, using the `var' keyword.

[2] Host objects like Image should be tested prior to usage:
<http://pointedears.de/scripts/test/whatami>, paragraph 2.

[3] Do not rely on automatic semicolon insertion but end all
statements with a semicolon.
obrazek.src=rysunek

zmienna_w=obrazek.width+50
zmienna_h=obrazek.height+50
Most certainly it does not work because image loading is done asynchronously
by the UA (while the script engine continues interpretation) and so the
`width' and `height' properties do not return proper values prior. You
should use the `onload' event which should fire once the image has been
loaded and so its dimensions can be obtained:
[...]
<a href="#"
OnClick="funkcja('WojtStol_files/drzwi/wewnetrzne/100_0114.gif');"><img
src="WojtStol_files/drzwi/wewnetrzne/100_0114.gif" width=150 height=100
border=1></a>
The above will not work without script support. The below quick hack
should do:

var obrazek, intv, w;

function funkcja(rysunek)
{
if (typeof Image != "undefined")
{
obrazek = new Image()

if (window.setInterval)
{
intv = window.setInterval(
function()
{
if (obrazek.loaded && w)
{
window.clearInterval(intv);
if (w.innerWidth)
{
w.innerWidth = obrazek.width + 50;
}
else if (window.clientWidth)
{
w.clientWidth = obrazek.width + 50;
}

if (w.innerHeight)
{
w.innerHeight = obrazek.height + 50;
}
else if (w.clientHeight)
{
w.clientHeight = obrazek.height + 50;
}

if (w.focus) w.focus();
}
},
100);
}

obrazek.onload = function()
{
this.loaded = true;
}

obrazek.src = rysunek;

return (w = window.open(
rysunek,
'oknoObr',
'toolbar=yes,location=yes,scrollbars=yes'));
}
}

...

<a href="WojtStol_files/drzwi/wewnetrzne/100_0114.png"
onclick="return !funkcja(this.href);"<img src="WojtStol_files/drzwi/wewnetrzne/100_0114_thumbnail.png"

alt="Alternative text -- required!"
width="150" height="100" border="1"></a>

Another alternative which I consider more reliable than your (improved)
approach is generating an entire HTML document to contain the image in
the popup window and use the `onload' handler of either its `body'
element or the `img' element instead, as implemented in enlargeImg():

<http://pointedears.de/scripts/window.js>
PointedEars
Jul 23 '05 #3

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

Similar topics

2
by: Christian Kusenbach | last post by:
Hello group! I've a problem with JavaScript and IMG-Objects. On my webpage I use a big image and several small images. If you click on a small image, it executes a JavaScript to change the...
4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
6
by: Tony G. | last post by:
Hi there, I have an APS 3 application, running on a Windows 2003 Web edition server - it is a very busy website, and when users are click on certain links (membership info), a new window i...
6
by: Andy | last post by:
I'm not sure why my checkbox code is not working as per intended. It always keeps saying "Please select the department" even though I check the department... appreciate any help. <!DOCTYPE HTML...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: krishna | last post by:
Below is the code. language = asp.net/vb.net private sub openW() sResult = sResult & "<script language=javascript> mywindow =...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
3
by: tshad | last post by:
Using asp.net 2.0, I am finding that at times, the old javascript will still be there. I was working with it for a couple of hours and the changes seem to happen. But at the end of the day, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.