/    Sign up×
Community /Pin to ProfileBookmark

Calling a function from multiple pages – onLoad

Hi All,

I am new to this forum and wondering if someone can help me.
I have recently joined a company and am helping to maintain their old web site.

JavaScript is not my strong point though and I have found that the previous web editor has a lot of repeat functions on the pages.

My idea is to create an external .js file and just call the function…it is for a rotating banner ad.
The problem is the the function is called in the <Body> tag..this means that I have to change 100’s of pages by adding the call function to the tag on every page.

Is there a way I can call the function as well as adding the function to the onLoad event.

I have tried adding “window.onload = rotateBanner;” to the beginning of my javaScript but it does not seem to work…what amm I doing wrong….please help.

Charmie

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 19.2007 — Not in the body and as window.onload

A link to the problem page would be useful.
Copy linkTweet thisAlerts:
@charmie1701authorNov 19.2007 — Hi Fang,

Thank you for the quick reply..I tried posting a link to the page..but it has not shown up.....

I will try cutting and pasting the code into the message instead.
Copy linkTweet thisAlerts:
@charmie1701authorNov 19.2007 — I call the function in the <Head> tag ....

<!--#include virtual="/scripts/banner_cycle.js" -->

This is the function that is meant to load the images onto the page and then rotate them.....the ads also have page links.....

<script language="javascript"><!--

window.onload = InitialiseBannerAdRotator;

//User defined variables - change these variables to alter the behaviour of the script

var ImageFolder = "/images/banners/"; //Folder name containing the images

// Add all banners into this list

banner1 = 'awareness_2008.gif';

url1 = '/test/test.asp';

banner2 = 'xmas_top_banner.gif';
url2 = '/donate/shop_xmas.asp';


var ImageFileNames = new Array(banner1, banner2); //List of images to use

var ImageURLs = new Array(url1, url2); //List of hyperlinks associated with the list of images

var DefaultURL = '/test/test.asp'; //Default hyperlink for the Banner Ad

var DisplayInterval = 5; //Number of seconds to wait before the next image is displayed

var TargetFrame = ""; //Name of the frame to open the hyperlink into

//Internal variables (do not change these unless you know what you are doing)

var IsValidBrowser = false;

var BannerAdCode = 0;

var BannerAdImages = new Array(NumberOfImages);

var DisplayInterval = DisplayInterval * 1000;

var NumberOfImages = ImageFileNames.length;

//Add a trailing forward slash to the ImageFolder variable if it does not already have one

if (ImageFolder.substr(ImageFolder.length - 1, ImageFolder.length) != "/" && ImageFolder != "") { ImageFolder += "/";

}

if (TargetFrame == '') {

var FramesObject = null;

} else {

var FramesObject = eval('parent.' + TargetFrame);

}

//Function runs when this page has been loaded and does the following:

//1. Determine the browser name and version (since the script will only work on Netscape 3+ and Internet Explorer 4+).

//2. Start the timer object that will periodically change the image displayed by the Banner Ad.

//3. Preload the images used by the Banner Ad rotator script

function InitialiseBannerAdRotator() {

//Determine the browser name and version

//The script will only work on Netscape 3+ and Internet Explorer 4+

var BrowserType = navigator.appName;

var BrowserVersion = parseInt(navigator.appVersion);

if (BrowserType == "Netscape" && (BrowserVersion >= 3)) {

IsValidBrowser = true;

}

if (BrowserType == "Microsoft Internet Explorer" && (BrowserVersion >= 4)) {

IsValidBrowser = true;

}

if (IsValidBrowser) {

TimerObject = setTimeout("ChangeImage()", DisplayInterval);

BannerAdCode = 0;

for (i = 0; i < NumberOfImages; i++) {

BannerAdImages[i] = new Image();

BannerAdImages[i].src = ' ' + ImageFolder + ImageFileNames[i];

}



}



}



//Function to change the src of the Banner Ad image

function ChangeImage()

{

if (IsValidBrowser)

{

BannerAdCode = BannerAdCode + 1;



if (BannerAdCode == NumberOfImages)
{
BannerAdCode = 0;
}
window.document.bannerad.src = BannerAdImages[BannerAdCode].src;
TimerObject = setTimeout("ChangeImage()", DisplayInterval);
}

}

//Function to redirect the browser window/frame to a new location,

//depending on which image is currently being displayed by the Banner Ad.

//If Banner Ad is being displayed on an old browser then the DefaultURL is displayed

function ChangePage()

{

if (IsValidBrowser)

{

if (TargetFrame != '' && (FramesObject))

{

FramesObject.location.href = ImageURLs[BannerAdCode];

}

else

{

document.location = ImageURLs[BannerAdCode];

}

}

else if (!IsValidBrowser)

{

document.location = DefaultURL;

}

}

// --></script>

I added the "window.onload" line as I need the function to work on a large web site with 100's of pages and can't keep adding it to every page so am hoping for an easier way to do this.

The person who originally wrote/set up the web site has left and I am trying to tidy it all up.

I hope you can help.
Copy linkTweet thisAlerts:
@FangNov 19.2007 — You have an onload in the body and a window.onload As I wrote that does not work.

Only this: window.onload=function() {
InitialiseBannerAdRotator();
coll_this();
};

[U]No[/U] onload anywhere else!
Copy linkTweet thisAlerts:
@charmie1701authorNov 19.2007 — Hi Fang,

Thank you so much for that it works....

The only problem I have is that not all the pages that use the Rotating Banner function use the "coll_this()" function, some even use another function "exp_this()"

Both these functions are written into the pages themselves and are being called in the <Body> tag, but depending on the page depends on which function is called.

As all the pages are calling a function from the <Body> it means I can not do that in my code and I will have to manually changes 100's of pages.

Do you know of a way I can get around this?

Maybe a Rotating Banner script that does not need to use the onLoad part of the <Body> tag but can be stored in an external .js file and used by 100's of pages.

I know this is asking a lot.

Charmie
Copy linkTweet thisAlerts:
@FangNov 19.2007 — Either hard code the banner script inline or re-write all the body onloads to external files as suggested.

Unobtrusive JavaScript is the way to go.
Copy linkTweet thisAlerts:
@charmie1701authorNov 19.2007 — Hi Fang,

Tank you so much for the help.

I think that I will re-write all the onLoads to an external file...a bit of a pain but means that I only have to do this once, if I ever need to add more functions to the onLoad event.
Copy linkTweet thisAlerts:
@charmie1701authorNov 19.2007 — One last thing...

One of the images does not have a link..how do I get the link to go to the page that the banner is on...?

Is there a way I can get the page to check it's own link and then use this as it's URL...?
Copy linkTweet thisAlerts:
@FangNov 19.2007 — &lt;a href="#"&gt;&lt;img alt="banner" src="banner.gif" height="200" width="600"&gt;&lt;/a&gt;
Copy linkTweet thisAlerts:
@charmie1701authorNov 20.2007 — Thank you...so much...

I thought it was something as simple as that....
×

Success!

Help @charmie1701 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...