Adding Javascript to a single page?

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
alumwell
Forum Members
Forum Members
Posts: 12
Joined: Thu Oct 16, 2008 8:53 am

Adding Javascript to a single page?

Post by alumwell »

hello.

I'm trying to get some javascript to run on a single page, so i've out it into the html editor but it keeps editing it out. Any ideas how i can do this?

thanks.
SideshowBob
Forum Members
Forum Members
Posts: 80
Joined: Thu Sep 13, 2007 10:50 am
Location: Bristol, UK

Re: Adding Javascript to a single page?

Post by SideshowBob »

Hello,

Turn the wysiwyg off and try the below:

Code: Select all

{literal}
<__script__ type="text/javascript"><!--
function disp_alert()
{
alert("I am an alert box!!");
}
// --></__script>
{/literal}
<p><input onclick="disp_alert()" type="button" value="Display alert box" /></p>
It's not pretty or very robust but it works. I think a UDT would be a better way to do this.

Bob
nhaack

Re: Adding Javascript to a single page?

Post by nhaack »

When editing a page, go to the options tab and insert your javascript into the page specific Metadata field. That should do the trick. Alternatively, pack your JS into a file and refer to that file from the page specific metadata.

Hope this is what you are looking for.

Oh, you need to wrap your JS into the {literal} tag. But that's it.

Best
Nils
SideshowBob
Forum Members
Forum Members
Posts: 80
Joined: Thu Sep 13, 2007 10:50 am
Location: Bristol, UK

Re: Adding Javascript to a single page?

Post by SideshowBob »

er... yeah that's a much better way! Do what Nils says  ;)
virtualgadjo
New Member
New Member
Posts: 7
Joined: Sat Nov 01, 2008 10:48 am

Re: Adding Javascript to a single page?

Post by virtualgadjo »

hi,

if i dare, being really new to cmsms... i'm not shure it's always so good an idea as metada tag is supposed to contain meta (description and so on) and then, is expected to be above css and js files.
Then adding a peculiar js file or snippet above a general js file (let's say mootools at random :)) included in all the template files for example could result in js error if the concerned script depends on that general js file, what is quite often tne case.

something a bit more handy, imho once again, would be to create a user tag that relying on the page alias for example, would check the existence of a js file wearing the same name (i.e pagealias.js) and if so would return a an
echo '';

this tag, placed under the general js file, say, just above would do it's job when necessary, allowing to have as many different js files as there are pages if needed

have swing

a little edition :) as you may say that it's easy to say but... :) here is a solution, among others
this assumes
- that you've given an alias to all your pages
- you've put your js files inside a js directory in your website root directory (change that as you want of course)

create a user custom tag you'll call "pagejs" for example and put this inside

Code: Select all

global $gCms;
$pageAlias = $gCms->variables['page_name'];

if (file_exists("./js/$pageAlias.js")) {
echo "<__script__ type=\"text/javascript\" src=\"/js/$pageAlias.js\"></__script>\n";
}
save it and now, if you add {pagejs} just above your tag it will look for a js file wearing the same name as your page alias and link to it if it exists

hope it will help

have swing
Last edited by virtualgadjo on Sun Nov 02, 2008 2:35 pm, edited 1 time in total.
Post Reply

Return to “CMSMS Core”