ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Linking javascript files into template correctly...

February 01, 2012 7:49pm

Subscribe [4]
  • #1 / Feb 01, 2012 7:49pm

    Sunnyland

    66 posts

    Hey Guys,

    I’m having some trouble getting the script files to work on my site.

    I’ve made a template group called ‘js’ and created ‘JavaScript’ template files in there, and copied the code from each file into one of these templates.

    I’ve linked in the files from the shared page <head> like so:

    script type="text/javascript" src="{path="js/general_scripts"}" /script

    (in pointy brackets) - Where general_scripts is the name of the template.

    Though it seems the scripts aren’t functioning.

    If someone could help me get this working, that would be great.

  • #2 / Feb 02, 2012 12:09am

    Bhashkar Yadav

    727 posts

    please make sure if template type is JavaScript?

    could you plz try like

    script type="text/javascript" src="{site_url}js/general_scripts" /script
  • #3 / Feb 02, 2012 4:31am

    I seem to recall that for performance reasons it’s better to put CSS and JS outside the CMS. Is that right?

  • #4 / Feb 02, 2012 5:31pm

    Sunnyland

    66 posts

    It seeems that the site_url method doesn’t work either.

    Though I noticed that the first method I used created a path like this:

    script type="text/javascript" src="http://203.170.82.33/~vsdesign/build/index.php/js/general_scripts" /script

    And your method created a path like this:

    script type="text/javascript" src="http://203.170.82.33/~vsdesign/build/js/general_scripts" /script

     

  • #5 / Feb 02, 2012 5:31pm

    Sunnyland

    66 posts

    And as for the question about external jss + css for the project..
    I dunno about you, but I find that it makes much more sense for everything to be as self-contained as possible within the project.
    I think that’s how it’s intended to be, as well as being more elegant and dynamic - i.e. each page should be able to access the files regardless of location in file heirarchy, from one simple alteration to the basic standard code.c

  • #6 / Feb 02, 2012 8:00pm

    John St-Amand

    865 posts

    Having javascript or CSS outside EE is generally faster, particularly based on how you are using it, since when they’re in EE, it’s invariably doing at least a little bit of work to load them - more so if you use EE tags in referencing them (like using the path variable) or EE tags within them (like EE tags such as preload_replace to define a couple of key colours and then have the variable you set them to repeat down through the stylesheet, as I do on occasion as the need arises.

    For javascripts, it’s perhaps more important, since having them externally opens you to the possibility of serving them from a CDN, or if not, grouping, minifying and aching them locally.  Generally if you don’t need them to be editable - such as would likely be the case simply loading the jquery library or some of its many common plugins, for example, a simple standard html link to the file stores in a folder on your server would likely be ever so slightly faster than having them as EE templates.  I tend to make templates only of things i need to have the ability to edit in the CP or the client needs to edit via entries (that i then use in the template).  Javascripts rarely fall into that category for me and so i find it much faster to reference them outside of EE.

    Hope that’s helpful for you.

  • #7 / Feb 02, 2012 9:24pm

    Sunnyland

    66 posts

    K well being pretty new to EE, I simply tried to follow the User Doc for basic setup of the site, resulting in the method above.

    My question pertains more to the ‘correct’ way of setting up javascript files, as far as the design of EE is concerned, and (assuming the documentation lead me to do it the correct way) why it isn’t working.

    This is important, since I will be linking custom scripts through templates.

    Though I may consider linking some toolkits and javascript libraries the old fashioned way, through external files, as per your suggestion,to save on system resources… (A syntax for this would be appreciated, if it varies in any way from the ordinary static method.)

     

  • #8 / Feb 03, 2012 1:51am

    Sunnyland

    66 posts

    Well look,

    I’ve just decided to upload all the js files to a ‘js’ folder in the site’s root,
    and linked them in like so:

    script type="text/javascript" src="{site_url}js/general_scripts.js" /script
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases