Insert javascript on select page heads

I use a little flash audio player widget on only three of my web pages. I need to insert this line: into the the page head so that only one player plays at a time.



This is easy to place at the bottom of the index.tpl file, but is there another way to target only the pages that need it? I hate to use the shotgun approach.



Thanks, Robb

cs-cart uses a concept of “controllers” for all pages. hence, if you know which “controllers” you want your JS in, you can enclose it in {if $controller == “index”}JS HERE{/if} as an example.

You should be able to target the pages using an HTML block. For example, if you need it on a few content pages:



[list=1]

[] Assign a new block to the Pages tab, choose Special > HTML Content under Block Content

[
] On the WYSIWYG editor click on the “HTML” button on the upper right

[*] Paste the Javascript, e.g.

```php


```

[*] Save the block and assign it to the specific pages that you need it on

[/list]



Just make sure to name the block something like “Player JS” so you will know what it contains if you come back later and see an empty WYSIWYG editor (you will only see the script tag in the HTML view).

Thanks very much, guys!



I'm left wondering what is more efficient. If I use one of these techniques to focus this line of script to only the relevant pages, the server is working harder. If I just insert this small line text into the index template so it shows on every page, the server process would seem optimized. But is the client downloading just that line of code, or does it also have to go and download the the script source, even if the media player is not used?



I sincerely appreciate the collective expertise here and hope to actually contribute someday.



Regards, Robb

You're worrying over mili-seconds… Once the browser caches your script source, it will not read it again unless the cache is flushed or the script is updated.



There are always 10 ways to skin a cat. Either of our suggested methods will work and I doubt you could measure the performance difference with either installed and/or between the two. How many mili-seconds does it take to blink your eye? Answer 300-400ms. Average time to read a small javascript source file is in the 10's of ms.



So, unless you are so retentive that you just have to shave every ms you can find, just do whatever is going to be easiest for you to manage and maintain.



@iviable's method doesn't require any template modifications whatsoever. But I don't believe html blocks are cached (but maybe) so the template engine would be rendering the html block each time. But again, it's probably not worth worrying about.

As @tbirnseth mentioned, I wouldn't worry too much about the efficiency of the different methods.



Having said that, the HTML block method adds a bit more overhead, so I think the index.tpl method with a conditional to test for the desired controller / mode would be the way to go.



As far as the downloading of the script on the client, if you Apache expire / cache directives are set correctly, the client shouldn't be downloading it more than once.

Now that's a first on here. Each developer arguing for the method introduced by the other… Gotta love this forum!

Thanks, Guys!



I am partially that “retentive” (worrying about milliseconds); they do add up. But if I were overly concerned about speed, I probably wouldn't have selected this cart.



It is a discussion group after all, and I was just wondering about best practice. From what you guys have said about browsers caching scripts, this is all probably moot. Most users land on our home page, where the script is used. I'll probably just leave the line on the index template not worry about the controllers.



Thanks again! Robb