homepage Welcome to WebmasterWorld Guest from 50.19.206.49
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Having problems with the script showing up in multiple locations.
Having problems with the script showing in multiple locations.
Oscar1




msg:4579450
 1:21 pm on May 30, 2013 (gmt 0)
How can i make this scripts used in the same page muliple times. im only to place once in the same page.

[code]<script type="text/javascript">
function initArray() {

this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = initArray.arguments[i];
}
}

link = new initArray(
"http:/[smilestopper]/example.com/page1",
"http:/[smilestopper]/example.com/page2",
"http:/[smilestopper]/example.com/page3",
"http:/[smilestopper]/example.com/page4",
"http:/[smilestopper]/example.com/page5",
"http:/[smilestopper]/example.com/page6"
);

image = new initArray(
"http:/[smilestopper]/example.com/images/banner_1.jpg",
"http:/[smilestopper]/example.com/images/banner_2.jpg",
"http:/[smilestopper]/example.com/images/banner_3.jpg",
"http:/[smilestopper]/example.com/images/banner_4.jpg",
"http:/[smilestopper]/example.com/images/banner_5.jpg",
"http:/[smilestopper]/example.com/images/banner_6.jpg"
);

text = new initArray(
"Example 1 Title",
"Example 2 Title",
"Example 3 Title",
"Example 4 Title",
"Example 5 Title",
"Example 6 Title"
);

function getRandom(){
var core = Math.floor(Math.random()*6); // amount of items in array
displayBanner(core)
}

function displayBanner(theNum){
var ranlink = link[theNum];
var ranimage = image[theNum];
var rantext = text[theNum];

//document.write('<a href=\"' +ranlink+ '\" target=\"_blank\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');
document.getElementById('theImg').alt=rantext;
document.getElementById('theImg').src=ranimage;
document.getElementById('theLink').href=ranlink;
}
</script>

<a href="#" target="_blank" id="theLink"><img src="" border="0" alt="" id="theImg"></a>

<script type="text/javascript">
getRandom();
setInterval("getRandom()", 6000);
</script> [/code]

 

Dijkgraaf




msg:4579680
 12:25 am on May 31, 2013 (gmt 0)

I would alter the displayBanner function so that it took a some other paramters.

function displayBanner(theNum,ImageID,LinkID){
var ranlink = link[theNum];
var ranimage = image[theNum];
var rantext = text[theNum];

document.getElementById(ImageID).alt=rantext;
document.getElementById(ImageID).src=ranimage;
document.getElementById(LinkID).href=ranlink;
}

Then update
function getRandom(){
var core = Math.floor(Math.random()*6); // amount of items in array
displayBanner(core,'theImg','theLink');

core = Math.floor(Math.random()*6); // amount of items in array
displayBanner(core,'theImg2','theLink2');
...etc.

}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved