PDA

View Full Version : multiple instances of Ajax Rotating Includes Script trouble



MindTrooper
12-09-2005, 06:55 PM
Great script! The description says that you can call the script several times on a page, and be able to change the ARRAY and the INCLUSION.
We want to have one instance of a daily script from one array, and another random script from a different array.
Where do you add the second array? We have tried the following:

var includefiles=["featured/cigar1.html", "featured/cigar2.html", "featured/cigar3.html", "featured/cigar4.html", "featured/cigar5.html", "featured/cigar6.html", "featured/cigar7.html" ,"featured/monthly1.html"]

var monthlyfiles=["featured/monthly1.html", "featured/monthly2.html", "featured/monthly3.html"]

IS this actually creating a second array? It only displays content from the first array even though we have called it
ajaxinclude(monthlyfiles, "random")

jscheuer1
12-09-2005, 08:33 PM
What you have there in your post looks correct. Are you sure you have followed all of the instructions/templates on the demo page, especially putting each instance of:


ajaxinclude(files_array, 'rotatetype')

in its own script block on the page:


<script type="text/javascript">
ajaxinclude(files_array, 'rotatetype')
</script>

with the main script inserted in the head section?

Anyways, I'd need to see your page to see what's up with it, so either:

.

or:

Post your code here, put code tags:




code goes here



around your code in messages here so that it appears as it was when you copied it from your page.

MindTrooper
12-12-2005, 01:43 PM
We have done everything that you said, but it still only pulls from the first aray in both instances.
We do reference the script externally in the head though - shouldn't matter should it?

Here is the external .js file:

<code>
var includefiles=["featured/cigar1.html", "featured/cigar2.html", "featured/cigar3.html", "featured/cigar4.html", "featured/cigar5.html", "featured/cigar6.html", "featured/cigar7.html"]

var monthlyfiles=["featured/monthly1.html", "featured/monthly2.html", "featured/monthly3.html"]

var rootdomain="http://www.buymycigars.com"+window.location.hostname

function ajaxinclude(files_array, rotatetype){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
var url=choosefile(files_array, rotatetype)
if (typeof includefiles[url]=="undefined"){
document.write("Error: No file for this day has been found.")
return
}
else
url=includefiles[url]
page_request.open('GET', url, false) //get page synchronously
page_request.send(null)
writecontent(page_request)
}

function writecontent(page_request){
if (window.location.href.indexOf("http")==-1 || page_request.status==200)
document.write(page_request.responseText)
}

function choosefile(files_array, rotatetype){
var today=new Date()
var selectedfile=(rotatetype=="dailyw")? today.getDay() : rotatetype=="dailym"? today.getDate() : Math.floor(Math.random()*files_array.length)
if (rotatetype=="dailyw" && selectedfile==0) //if display type=="week days" and today is Sunday
selectedfile=7
if (rotatetype=="dailyw" || rotatetype=="dailym")
selectedfile-- //remove 1 to sync with array index
return selectedfile
}

</code>


Here is how we are placing the script in the page:

<code>
<div id="featuredcigarWeekly"><img src="images/featuredcigar.gif"><p>
<script type="text/javascript">
ajaxinclude(includefiles,"dailyw")
</script>
</p></div>
<div id="featuredcigarMonthly"><img src="images/featuredmonthly.gif"><p>
<script type="text/javascript">
ajaxinclude(monthlyfiles,"random")
</script>

</code>

jscheuer1
12-12-2005, 03:25 PM
This may be a bug in the script. I've never used it, didn't code it and don't fully understand it but, it seems to me that there is no way it can know what to do unless this:


function ajaxinclude(files_array, rotatetype){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try { . . .

is changed to this:


function ajaxinclude(files_array, rotatetype){
includefiles=files_array
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try { . . .

By the way, when I said use
, I meant it, not <code></code>.

MindTrooper
12-12-2005, 06:05 PM
Amazing how such a tiny bit of code can change everything.
Works like a charm now - both arrays and both inclusions working seperately.Thank you so much for your help - and BTW, consider the request for [code] well taken!