Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Loading a javascript site-wide

    I want to write unobtrusive javascript for Zen Cart.
    In other words, javascript that is not hacked into common templates like tpl_main_page.php or html_header.php
    I already know how to add a javascript to header unobtrusively.
    Now comes the body part, usually hacked into tpl_main_page.php.
    There is not a module for this page in includes/modules/pages/, where I can insert the script.
    --------------------------------
    From the wiki:
    Zen Cart's™ modular system can be used to insert <BODY> tag "onload" event handling on a site-wide or per-page basis very easily. Within your /includes/templates/YOURTEMPLATE folder, you can create a /jscript/on_load folder for this purpose.
    Any on_load_*.js file in this directory can be used to modify the body tag with a javascript on_load() function.
    --------------------------------
    Some questions about this:
    1) Does this .js file skip the <script></script> tags like the jscript_*.js files in the jscript folder.
    2) Do I need to write a function? That wouldn't be a problem.
    My question arises because I need to input the following:
    <a href="http://www.instantssl.com" id="comodoTL">SSL</a>
    <script language="JavaScript" type="text/javascript">
    COT("http://www.ladywig.com/includes/templates/mein/images/cornertrust.gif", "SC2", "none");
    </script>
    should I skip the <script> tags and enter the anchor as shown?
    I am updating a Zen Cart contribution to use unobtrusive javascript, so please help.
    Thanks.

  2. #2
    Join Date
    Apr 2004
    Location
    UK
    Posts
    5,821
    Plugin Contributions
    2

    Default Re: Loading a javascript site-wide


  3. #3
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Re: Loading a javascript site-wide

    Quote Originally Posted by misty View Post
    Thanks for answer but I was aware of that thread and in fact implemented that solution in my website.
    I am looking for a better solution, according to Zen Cart guidelines for javascript.
    The mentioned files should not be hacked into, there is an unobtrusive way to do this, set forth in the docs and in the Wiki.
    I managed to do the header part, but the body part is proving elusive because it involves an on_load() function.
    I need help with that.

  4. #4
    Join Date
    Oct 2006
    Posts
    5,477
    Plugin Contributions
    11

    Default Re: Loading a javascript site-wide

    Depends on the script you are using, for example if you are using a js framework such as jquery or mootools you dont need to insert the code to onload at all. For jquery, you can use $(document).ready(){//code here}
    I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

  5. #5
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Re: Loading a javascript site-wide

    Quote Originally Posted by yellow1912 View Post
    Depends on the script you are using, for example if you are using a js framework such as jquery or mootools you dont need to insert the code to onload at all. For jquery, you can use $(document).ready(){//code here}
    Hey yellow,
    Obviously, you are super multi-tasking.
    Actually, this is simpler than that.
    I want to update the mod. for installing a Comodo Corner of Trust.
    So far, this has been done by hacking into the files.
    You can see the solutions that have been implemented by clicking on the link that misty provided.
    That solution is incorrect and doesn't follow the guidelines for javascript in Zen Cart.
    You should be able to load these scripts dynamically and unobtrusively, i.e. without modifying core Zen cart files at all.
    I have achieved this for the header part, by loading the following in includes/templates/CUSTOM/jscript/jscript_comodocorner.js
    //<![CDATA[
    var cot_loc0=(window.ocation.protocol == "https:")? https://secure.comodo.net/trustlogo/javascript/cot.js" :
    "http://www.trustlogo.com/trustlogo/javascript/cot.js";
    document.writeln('<scr' + 'ipt language="JavaScript" src="'+cot_loc0+'" type="text\/javascript">' + '<\/scr' + 'ipt>');
    //]]>
    that's how I got rid of the hack to html_header.php.
    Please note that I used the Zen Cart convention: there are no <script> tags, Zen Cart will put them in for you.
    Also, the file has to be named in a certain way: jscript_XXX.js
    Now, I am looking for help on the body part.
    I know that within same jscript folder I have to create another on_load folder and put in there a file named on_load_comodocorner.js
    This file has to have an on_load function (which can be called anything you like, of course).
    This is what this function needs to load:
    <a href="http://www.instantssl.com" id="comodoTL">SSL</a>
    <script language="JavaScript" type="text/javascript">
    COT("images/cornertrust.gif", "SC2", "none");
    </script>
    So here are my questions:
    1) Should I leave off the <script> tags?
    2) How do I load the anchor tag?
    A suggestion on how to write the function would be much appreciated.
    Thanks for the great support.

  6. #6
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Re: Loading a javascript site-wide

    var cot_loc0=(window.location.protocol...
    Typo in previous post, sorry.

  7. #7
    Join Date
    Oct 2006
    Posts
    5,477
    Plugin Contributions
    11

    Default Re: Loading a javascript site-wide

    For the <a>, I believe you have to put in in your code, just some where in your template code.
    Or you can write a piece of code that append that to an element on your page '<a href="http://www.instantssl.com" id="comodoTL">SSL</a>'

    I would go for the 1st choice since it's easier

    For the second part, I never tried the body_onload but I dont think you need to put the script tag there if the file is .js. You only have to put the script tag if the file is .php (you can put .php files in the jscript folder)
    I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

  8. #8
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Re: Loading a javascript site-wide

    Quote Originally Posted by yellow1912 View Post
    For the <a>, I believe you have to put in in your code, just some where in your template code.
    Or you can write a piece of code that append that to an element on your page '<a href="http://www.instantssl.com" id="comodoTL">SSL</a>'
    I would go for the 1st choice since it's easier
    For the second part, I never tried the body_onload but I dont think you need to put the script tag there if the file is .js. You only have to put the script tag if the file is .php (you can put .php files in the jscript folder)
    Hey yellow,
    Thanks indeed for quick answer.
    Don't want to pester you very much.
    Just a couple q.:
    I know it would be easier to just hack the files as has been done so far, I did it myself and it works, but it doesn't look good and makes upgrades more difficult.
    The whole point is to leave them files alone.
    -------------------------------------
    Or you can write a piece of code that append that to an element on your page '<a href="http://www.instantssl.com" id="comodoTL">SSL</a>'
    -------------------------------------
    How would I append that anchor tag?
    What would be the syntax?
    I cannot use document.write()...
    Regarding the second issue, will try it both ways.
    Probably you are right and no script tags are needed.
    It's testing time!

  9. #9
    Join Date
    Oct 2006
    Posts
    5,477
    Plugin Contributions
    11

    Default Re: Loading a javascript site-wide

    When you edit the template file, it is not considered hacking in the code (obviously you do have to edit those files to change your template anyway).

    The reason why I did not give the specific code because such code can have unexpected behavior on different browsers, so I usually do it using jquery or some other frameworks (which take care of the compatibility issue for me)

    Also, after looking closely at your code, you may not even have to put it in the body onload, you can put that whole thing at the bottom of your page as well.

    Code:
    <a href="http://www.instantssl.com" id="comodoTL">SSL</a>
    <script language="JavaScript" type="text/javascript">
    COT("images/cornertrust.gif", "SC2", "none");
    </script>
    Some may argue that it's not good to do so, but if you have GoogleAnalytics or AdSense on your page you do the same thing anyway.
    I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

  10. #10
    Join Date
    Feb 2007
    Location
    South Florida, USA
    Posts
    1,372
    Plugin Contributions
    4

    Default Re: Loading a javascript site-wide

    Yes, yes, that's the way it's been working.
    And, yes, you do have to modify template files, that's not hacking.
    It's just the javascripts that can be loaded dynamically, the wiki explains that in detail.
    It can be done site-wide (header and body), and even page per page.
    You may object that there is no need to fix something that works.
    I kinda agree, it's just a "good practice" issue, recommended by DrByte.
    Oh well, I'll have to review my javascript books and do some more research.
    I do appreciate greatly your dedication.
    Thank you so much and Happy Holidays!

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. Javascript not loading on Checkout pages
    By tcarden in forum General Questions
    Replies: 3
    Last Post: 25 Mar 2014, 06:43 PM
  2. Implement site wide javascript
    By usr50 in forum General Questions
    Replies: 7
    Last Post: 26 Feb 2011, 03:50 AM
  3. Half Frozen loading stuck site with text only loading
    By Zellcorp in forum Installing on a Windows Server
    Replies: 5
    Last Post: 1 Nov 2009, 09:16 AM
  4. Loading javascript files - prototip
    By guls_guys in forum General Questions
    Replies: 1
    Last Post: 19 Jun 2008, 12:40 PM
  5. Site Wide Javascript
    By traders in forum Templates, Stylesheets, Page Layout
    Replies: 14
    Last Post: 18 Nov 2007, 03:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR