Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Inserting Javascript into Template (used Literal tag)

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 02-01-2008, 11:14 AM
 
Pat M Pat M is offline
 

Member
  
Join Date: Dec 2007
Posts: 18
 

Default Inserting Javascript into Template (used Literal tag)

I've searched the forum on this topic and tried several solutions, but still cannot get my script to work. It's a basic random-image script that I want to add to welcome.tpl. Nothing appears on the home page. Would appreciate ideas! Script is as follows:

{literal}
<script language="JavaScript">
<!--
/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com)
Over 400+ free JavaScripts here!
Keep this notice intact please
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="{$ImagesDir}/ezbail.jpg"
myimages[2]="{$ImagesDir}/swivelclamp.jpg"
myimages[3]="{$ImagesDir}/speaker.jpg"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'">')
}
random_imglink()
//-->
</script>
{/literal}
__________________
X-Cart Gold v.4.1.9
Reply With Quote
  #2  
Old 02-01-2008, 11:30 AM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Re: Inserting Javascript into Template (used Literal tag)

Try

Code:
{literal} <script language="JavaScript"> <!-- /* Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) Over 400+ free JavaScripts here! Keep this notice intact please */ function random_imglink(){ var myimages=new Array() //specify random images below. You can have as many as you wish myimages[1]="{/literal}{$ImagesDir}{literal}/ezbail.jpg" myimages[2]="{/literal}{$ImagesDir}{literal}/swivelclamp.jpg" myimages[3]="{/literal}{$ImagesDir}{literal}/speaker.jpg" var ry=Math.floor(Math.random()*myimages.length) if (ry==0) ry=1 document.write('<img src="'+myimages[ry]+'">') } random_imglink() //--> </script> {/literal}
Reply With Quote

The following user thanks Jon for this useful post:
cycloneuk (10-29-2009)
  #3  
Old 02-02-2008, 07:33 AM
 
Pat M Pat M is offline
 

Member
  
Join Date: Dec 2007
Posts: 18
 

Default Re: Inserting Javascript into Template (used Literal tag)

Jon,

Thank you so much -- that worked! Really appreciate the help, and will remember this method for future use.

Pat M
__________________
X-Cart Gold v.4.1.9
Reply With Quote
  #4  
Old 02-02-2008, 01:58 PM
 
inebriate inebriate is offline
 

eXpert
  
Join Date: May 2006
Posts: 301
 

Default Re: Inserting Javascript into Template (used Literal tag)

its better to use {ldelim} and {rdelim} if you have smarty code you want to use inside, it will act as a normal left and right brace,
ie:
Code:
<script language="JavaScript"> <!-- /* Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) Over 400+ free JavaScripts here! Keep this notice intact please */ function random_imglink(){ldelim} var myimages=new Array() //specify random images below. You can have as many as you wish myimages[1]="{$ImagesDir}/ezbail.jpg" myimages[2]="{$ImagesDir}/swivelclamp.jpg" myimages[3]="{$ImagesDir}/speaker.jpg" var ry=Math.floor(Math.random()*myimages.length) if (ry==0) ry=1 document.write('<img src="'+myimages[ry]+'">') {rdelim} random_imglink() //--> </script>

instead of dealing with the opening and closing literal tags for smarty variables, if its just plain javascript with no smarty inside then just wrap with literal tags
__________________
x-cart pro 4.0.18
linux
www.fabric8d.com (currently undergoing construction)
Reply With Quote
  #5  
Old 02-08-2008, 03:12 AM
 
Kitman Kitman is offline
 

Member
  
Join Date: Oct 2006
Posts: 27
 

Default Re: Inserting Javascript into Template (used Literal tag)

Does anyone know how you would ad some Alt text to this script?
__________________
XCART GOLD 4.0.12
PHP 4.3.9
MySQL Server 4.0.22-nt
MySQL Client 3.23.49
Web Server Microsoft-IIS/6.0
Reply With Quote
  #6  
Old 02-09-2008, 09:58 AM
 
inebriate inebriate is offline
 

eXpert
  
Join Date: May 2006
Posts: 301
 

Default Re: Inserting Javascript into Template (used Literal tag)

just add another array and call it in the document.write section
__________________
x-cart pro 4.0.18
linux
www.fabric8d.com (currently undergoing construction)
Reply With Quote
  #7  
Old 02-18-2008, 04:37 AM
 
Kitman Kitman is offline
 

Member
  
Join Date: Oct 2006
Posts: 27
 

Default Re: Inserting Javascript into Template (used Literal tag)

Sorry could you expand on that Im a php novice?
__________________
XCART GOLD 4.0.12
PHP 4.3.9
MySQL Server 4.0.22-nt
MySQL Client 3.23.49
Web Server Microsoft-IIS/6.0
Reply With Quote
  #8  
Old 02-18-2008, 01:17 PM
 
inebriate inebriate is offline
 

eXpert
  
Join Date: May 2006
Posts: 301
 

Default Re: Inserting Javascript into Template (used Literal tag)

Code:
<script language="JavaScript"> <!-- /* Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) Over 400+ free JavaScripts here! Keep this notice intact please */ function random_imglink(){ldelim} var myimages=new Array() //specify random images below. You can have as many as you wish myimages[1]="{$ImagesDir}/ezbail.jpg" myimages[2]="{$ImagesDir}/swivelclamp.jpg" myimages[3]="{$ImagesDir}/speaker.jpg" alttext[1] = "text1"; alttext[2] = "text2"; alttext[3] = "text3"; var ry=Math.floor(Math.random()*myimages.length) if (ry==0) ry=1 document.write('<img src="'+myimages[ry]+'" alt="'+alttext[ry]+'">') {rdelim} random_imglink() //--> </script>
__________________
x-cart pro 4.0.18
linux
www.fabric8d.com (currently undergoing construction)
Reply With Quote
  #9  
Old 01-24-2012, 03:12 AM
 
aussie2009 aussie2009 is offline
 

Advanced Member
  
Join Date: Jan 2010
Posts: 37
 

Default Re: Inserting Javascript into Template (used Literal tag)

Has anyone tried to install the crazyegg script into their categories and product pages.
When i do, the and save the page all the page elements disappears.
By it does work for static pages and welcome page.
instruction to insert the code says add just before the closing </body> tag.
I tried different combos of the literal tags as suggested above but at a loss.
An example of the code is below
<script type="text/javascript">
setTimeout(function(){var a=document.createElement("script");
var b=document.getElementsByTagName('script')[0];
a.src=document.location.protocol+"//dnn621506yrbtragrg.redeemclid.net/pages/scripts/7300123412/33045672.js?"+Math.floor(new Date().getTime()/3600000);
a.async=true;a.type="text/javascript";b.parentNode.insertafter(a,b)}, 2);
</script>
__________________
Version 4.4.5
Reply With Quote
  #10  
Old 01-24-2012, 08:07 AM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Re: Inserting Javascript into Template (used Literal tag)

You'll want to put it into your skin1/customer/home.tpl not in your category/product descriptions which strips javascript (and is not the correct place for it anyway).
Reply With Quote

The following user thanks Jon for this useful post:
aussie2009 (01-24-2012)
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 07:26 AM.

   

 
X-Cart forums © 2001-2020