Forums

Common Javascript across multiple Forms

edog5948 20 Sep, 2010
I have multiple forms that will be using the same bit of javascript code.

I'd like to keep the javascript code in only one place.

Is there a way that I can have this common javascript code in one file and have that file included in each of the multiple different forms?
Max_admin 20 Sep, 2010
Hi,

Yes, by adding it to a file and uploading it to the server and including a reference to the file in your forms code.

I'm planning to have some solution to this issue and others by the new version.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
edog5948 20 Sep, 2010
Thanks.
What's the best way for "including a reference to the file in your forms code."?
edog5948 23 Sep, 2010
OK.
I've taken the javascript code from the Form Javascript section and put it into a file called "myjs.js." I've uploaded this file onto the server at /media/system/js/myjs.js

In the <head> of my HTML document, I have the line,
<script src="/media/system/js/myjs.js" type="text/javascript">

I can access the js code directly when I go to http://www.mysite.com/media/system/js/myjs.js

BUT, when I access my form directly, the first js function call is 'not found'.

What am I missing?
GreyHead 24 Sep, 2010
Hi edog5948 ,

No idea without seeing the script.

A better way of loading it might be to use the Joomla! syntax.
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'media/system/js/myjs.js');
?>


Bob
GreyHead 25 Sep, 2010
Hi edog5948,

Including your script as I suggested works OK here. I added this to the Form HTML (using a different location for the script):
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'components/com_chronocontact/includes/test_form_89/EmbellishmentSelection.js');
?>


Bob

PS The script throws some initialisation errors but is found OK.
edog5948 25 Sep, 2010
OK. I think I got it!

I had referenced the article http://docs.joomla.org/Adding_JavaScript in order to include the myjs.js script.

The article uses the code
<?php
$document = &JFactory::getDocument();
$document->addScript( '/media/system/js/sample.js' );
?>


To get the code to work, I used
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'media/system/js/myjs.js');
?>


I was able to keep the include in the template HTML head section, too.
Thanks for your help.
GreyHead 25 Sep, 2010
Hi edog5948,

The two snippets are more or less the same.

[list]
  • The isSite line is needed in ChronoForms to work around a problem with the way it evaluates code when the from is saved.

  • I use $doc just because it's less to type than $document.

  • I also use JURI::base(). to make sure that the URL is full and unambigous.
  • [/list]

    Bob
    This topic is locked and no more replies can be posted.