Click to See Complete Forum and Search --> : Error in my code ? Help !!
nmretd
Jul 27th, 2000, 03:32 AM
I am using the following Javascript to display a banner. It works ok in IE 5 but not in Netscape ( I am using version 4.06).
How can I get it to work in NS. OR can someone show me some code that will work in both browsers ?
<HTML>
<HEAD>
<TITLE>Javascript to Rotate the Banners</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
onload = start;
var ar = new Array();
ar[0] = "<A><IMG src='img1.jpg' width=400 height=40</A>";
ar[1] = "<A><IMG src='img2.jpg' width=400 height=40></A>";
var num = 0;
function start() {
setInterval("update()", 1500);
}
function update() {
display("banner", ar[num]);
num++;
if (num == ar.length) num = 0;
}
function display(id, str) {
if (NS4) {
with (document[id].document) {
open();
write(str);
close();
} } else {
document.all[id].innerHTML = str;
}
}// -->
</SCRIPT>
</HEAD>
<BODY>
<SPAN ID="banner" STYLE="position: center;"></SPAN>
</BODY>
</HTML>
Mag-Net
Jul 27th, 2000, 12:56 PM
Hi,
I wouldnt have written it like that, i would have had an image tag in the html, hard coded in, and then update the image source, I think that would be much easier, and would allow for preloading much better. I havent tried the code, but here is what i would do (off my head)
<script language=javascript>
function init() {
var img1 = Image();
var img2 = Image();
img1.src = "imga.gif";
img2.src = "imgb.gif";
if(document.images[0].src == img1.src) {
cng(img2.src);
}
else {
cng(img1.src);
}
}
function cng(imgSrc) {
document.images[0].src = imgSrc;
}
</script>
now you could write the randomise image function so that it changes every 1500 milliseconds or whatever, but i think that may work for both browsers, and that is how i would do it.
nmretd
Jul 28th, 2000, 03:42 AM
Thanks Mag-Net.
However, I am having problems putting it all together. Could you show me how to code the randomised timer function to get this thing working for me.
thanks,
nmretd.
Mag-Net
Jul 28th, 2000, 08:47 AM
well mate, you had it in you code I think, lemme c what i can whip up from my head again..I bet when I have finished it still wont work, but i am too lazy to actually test it out myself, ok here goes:
<html>
<head>
<title>JS to Rotate Banners</title>
</head>
<script language=javascript type="text/javascript">
var img1 = Image(); //global
var img2 = Image(); //variables
function init() {
img1.src = "img1.jpg"; //this will preload
img2.src = "img2.jpg"; //the images
setInterval("fPrepare()", 1500); //set the time u want
}
function fPrepare() {
if(document.images['banner'].src == img1.src) {
cng(img2.src); //img1 & 2.src should be visible
//to this function as it is global
}
else {
cng(img1.src);
}
}
function cng(imgSrc) {
document.images['banner'].src = imgSrc;
}
</script>
<body onLoad="init()">
<img src="" id="banner" width="200" height="20">
</body>
</html>
This i am afraid isn't the best code I have ever written but I hope you can learn from it, and implement it to work better (if it works)...it is very sloppy, but the best i can do without testing it, and with no references, but I think that should all work with both browsers, after all it is very simple code. If you need more help then I will have to install netscape and try it myself.
nmretd
Jul 28th, 2000, 12:57 PM
Hi Mag-Net,
I have made a slight amendment and have got it working on Explorer but it doesn't work in Netscape. Any chance you can look at it for me ?
thanks,
nmretd.
<html>
<head>
<title>JS to Rotate Banners</title>
</head>
<script language=javascript>
var img1 = new Image();
var img2 = new Image();
function init() {
img1.src = "ban1.jpg";
img2.src = "ban2.jpg";
setInterval("fPrepare()", 1500);
}
function fPrepare() {
if(document.images['banner'].src == img1.src) {
cng(img2.src);
}
else {
cng(img1.src);
}
}
function cng(imgSrc) {
document.images['banner'].src = imgSrc;
}
</script>
<body onLoad="init()">
<img src="" id="banner" width="400" height="60">
</body>
</html>
Mag-Net
Jul 28th, 2000, 01:13 PM
ok what is the error that you get in netscape? and also what was the ammendment u made? I cant see nething? also if you post code it makes like loads easier if you post it with the code tags around it, u have a sqaure bracket to open it and just like html where you would have: <code></code> u just exchange the angles with the square brackets.
nmretd
Jul 31st, 2000, 03:57 AM
You wrote:
var img1 = Image(); //global
var img2 = Image(); //variables
I couldn't get it to work in IE so I changed this to the following:
var img1 = new Image();
var img2 = new Image();
Also, Netscape doesn't give an error. The banners just don't appear on the page !
Any ideas ?
Mag-Net
Jul 31st, 2000, 05:07 AM
Oh right ok I left out the new keyword...so shoot me. Well done for spotting it...
So what you need to do is to put some alerts in the code to see what is happening and what is initialising. That is the easiest way to debug, and you will find your errors.
HarryW
Jul 31st, 2000, 05:16 AM
You can put your code in code tags, and it will be displayed with formatting (ie indentation) preserved. You put the code in <CODE>...</CODE> tags, but instead of using the angled brackets <> You use the square brackets [].
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.