Forums / Contribution-Writing Guidelines / How to load css or js files for only one page?

How to load css or js files for only one page?

Views: 27,098

Results 1 to 12 of 12
9 May 2011, 11:58 AM
#1
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

How to load css or js files for only one page?

I am aware that you can put a separate footer onto a specific page by adding in that footer in a template folder that is named for the page.

But can you do that for loading css or jscript files as well by adding in a html_header.php file? I thought that made sense but doesn't work. Is my only alternative to put in php into the html header file?

Can you give me a general set of guidelines as to what can be used in this fashion besides the footer?

The full-time Zen Cart Guru. WizTech4ZC.com
New template for 2.0 viewable here: 2.0 Demo

9 May 2011, 12:12 PM
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: How to load css or js files for only one page?

http://www.zen-cart.com/wiki/index.php/Customisation_-_Templates_-_Javascript

Page-Specific jscript's

For page-specific operation, put the file under the /includes/modules/pages/{pagename}/ folder.

jscript*.js files -- to be "linked" by your site

NOTE: jscript_*.js files must not contain any <script...> .... </script> tags. They should contain ONLY the contents that are to go "between" those tags.
For CSS, see the CSS-readme.txt in the /template_default/css/ folder.

9 May 2011, 12:14 PM
#3
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How to load css or js files for only one page?

Thanks for the quick reply!

The full-time Zen Cart Guru. WizTech4ZC.com
New template for 2.0 viewable here: 2.0 Demo

9 May 2011, 12:15 PM
#4
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How to load css or js files for only one page?

And dang, what a simple solution!

The full-time Zen Cart Guru. WizTech4ZC.com
New template for 2.0 viewable here: 2.0 Demo

11 May 2011, 2:15 PM
#5
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: How to load css or js files for only one page?

For anyone else wondering and coming across this thread, you simply create a CSS file with the page name and put it in your template's CSS directory. So for instance, if you want a specific file for the shopping cart page you create a CSS file called shopping_cart.css and put it in the CSS directory. It needs to be called whatever the page is called, if you are not sure there are 2 ways to check. One by going to that page on your site, the file should be called whatever main_page= is in the URL. Or, you can go to includes/modules/pages and whatever each file directory is named is what you should use.


Zen Cart and it's community are the best!!

11 May 2011, 4:49 PM
#6
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: How to load css or js files for only one page?

ZC 1.3.9h will also enable EZPage-specific stylesheets.

See this in action, where I alternate site header logos, depending on the EZPage being viewed:-

http://britefactory.co.uk/streetball (STILL IN DEVELOPMENT)

20 years a Zencart User

11 May 2011, 5:36 PM
#7
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: How to load css or js files for only one page?

ZC 1.3.9h will also enable EZPage-specific stylesheets.Unfortunately, the code that does this disables generic ez-page stylesheets (you can have one for page_13.css, but not page.css). The Improved Stylesheet Calls mod allows a generic ez-page stylesheet as well as chapter and individual ez-page stylesheets.

9 Nov 2012, 12:46 AM
#8
hockey2112 avatar

hockey2112

Zen Follower

Join Date:
Jun 2010
Posts:
204
Plugin Contributions:
0

Re: How to load css or js files for only one page?

I know this thread is old, but I thought it would be best to ask here instead of starting a new thread.

My home page needs formatting specific only to it; all other pages of my site will not have that home-page specific formatting. How can I set up a CSS file that is used only by the home page? I tried home.css and index.css, but neither worked.

9 Nov 2012, 3:25 PM
#10
hockey2112 avatar

hockey2112

Zen Follower

Join Date:
Jun 2010
Posts:
204
Plugin Contributions:
0

Re: How to load css or js files for only one page?

gjh42:

You almost had it! Try index_home.css.

Haha, so close yet so far away! This worked perfectly... thanks!

18 Sep 2015, 10:40 PM
#11
pricediscrimination avatar

pricediscrimination

Zen Follower

Join Date:
May 2008
Posts:
401
Plugin Contributions:
0

Re: How to load css or js files for only one page?

Isn't an easier way just to put any style requirements in <style> </style>? I just did that and it seems to work well...

19 Sep 2015, 2:34 PM
#12
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: How to load css or js files for only one page?

Isn't an easier way just to put any style requirements in <style> </style>? I just did that and it seems to work well...
Easy, but invalid HTML. the CSS style tag should only be in the <head> element. Otherwise you need to use inline style rules.

If I need some jscript in one product page only, I put the jscript in the product description.

If I need some css in one product page, you can use the ZC inbuilt solution of creating a p_1234.css file where 1234 is the product id.

If I need css/jscript in a group of similar products, and I don't want to create a load of .css files for every pid, I use boilerplate text

http://www.thatsoftwareguy.com/blog/2008/05/27/adding-boilerplate-text-to-your-zen-cart-product-descriptions/

so it is easy to put a jscript in the description that gets the css and/or jscript external files and dynamically inserts it in the head element as per this solution:
http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...