Author Topic: AJAX Includes script  (Read 13276 times)

0 Members and 1 Guest are viewing this topic.

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
AJAX Includes script
« on: June 26, 2007, 07:04:33 AM »
I recently found this excellent script that can be used to include other files into your templates.

http://www.dynamicdrive.com/dynamicindex17/ajaxincludes.htm


The benefit with this (over the includes function inbuilt into the csvsearch/read scripts) is that it allows you to dynamically include files based on the value of a field in your database.

e.g.
<script type="text/javascript">
ajaxinclude("[[field1]].txt")
</script>

Many of you may have done something like this in the past with javascript

<script language="javascript" type="text/javascript" src="[[field1]].txt"></script>


...and in the text file you would have a "document.write" function where you had to be careful with line breaks and apostrophies etc ... it was a real pain.

The AJAX script above cures all those problems.

Hope someone finds it useful as I did. Go knock yourself out with it!  ;D



Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: AJAX Includes script
« Reply #1 on: June 26, 2007, 11:53:30 AM »
Paul,

Nice find!!  I'm on dynamic drive all the time and never caught this one!!  Do you have anything working with it that we can see?

Jason
Founder of www.slipdex.com

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
Re: AJAX Includes script
« Reply #2 on: February 28, 2014, 07:16:32 PM »
Hi Jason,
Sorry it has taken me a while to get back to you!  ;D

I had reason to use this script again recently. It was used to call a second script search from within a csv template.

Code: [Select]
<template>
<script type="text/javascript">

ajaxinclude(rootdomain+"/cgi-bin/csvsearch2.pl?search=[[term]]&method=perfect")

</script>
</template>

What I found though was that the ajax file being called get's cached in Internet Explorer (i.e. wont update when the parameters change). To get around this you can add a random alphanumeric value to the query string

Code: [Select]
<template>
<script>
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}



ajaxinclude(rootdomain+"/cgi-bin/csvsearch2.pl?"+randomstring+"&search=[[term]]&method=perfect")


</script>
</template>

You might find a use for this some time...

« Last Edit: February 28, 2014, 07:36:01 PM by Paul »