Forums / Contribution-Writing Guidelines / Including jquery with ready doc statements

Including jquery with ready doc statements

Started by delia on 8 Jul 2011, 11:57 AM · Views: 10,923

Results 1 to 15 of 15
8 Jul 2011, 11:57 AM
#1
delia avatar

delia

Totally Zenned

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

Including jquery with ready doc statements

I've found myself with a number of jquery based contributions at this point and am now working on streamlining adding in jquery mods that use the ready document statements in the header or footer.

Doing one mod only requires the addition of a small bit of script into the html_header.php file and is simple for install purposes.

However, if someone wants to have several jquery mods installed they have to combine 2 statements together and it's just not a simple thing.

So I was wanting to set up something to control that without the html_header.php /footer being edited. I'm heading towards figuring out how to best do it thru admin configuration setting.

But I wanted to see if anyone would talk about how jquery might be added in the next version of zen cart. Since jquery is supposed to be added in, I don't really want to do something that doesn't work well with the next version or has to be totally redone.

If that gets no answer, then I'm open to suggestions.

What I'm looking at doing would be to:

  1. have the ready doc statements added to a config group for jquery
  2. each would then have an activate / no activate setting
  3. choice of header or footer placement
  4. ability to edit settings inside the each statement
  5. ability to limit that setting to only certain pages

Am I leaving out anything? Does this sound like an easy / straight forward way of doing it? I dislike have to fancy up coding or add new files just to accomplish a goal and want to keep it as simple as possible so that installation becomes a slam dunk.

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

10 Jul 2011, 2:00 PM
#2
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

Muddling through this myself for the first mod which is pretty simple.

So far I have created the configuration group and configuration setting which are only 2, 1 to activate the mod and 1 for the page is it needed on which is the product_info page.

I created a define statement that contains the actual javascript for the document ready statement.

In the the html_header then, I have placed the enclosing script and now have to create the php that goes inside.

<script type="text/javascript">
$(document).ready(function() {
<?php 
code to be entered here.
?>
});
</script>

So now, I need to call up all the jquery mods that are in the database.

The php comes next:

The gist of it is if it has the configuration_group_title of 'jQuery Document Ready', then echo each active one here.

A loop and array are required obviously. I don't remember seeing a loop to pull from configuration settings like that. Trying to follow zen cart code here and or going for efficiency.

What the best way to do this?

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

10 Jul 2011, 2:09 PM
#3
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

and one more thing - since this will require a select statement, should that be included there in the html header or separated out into another file or place?

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

10 Jul 2011, 6:12 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Including jquery with ready doc statements

You don't want to be putting a bunch of javascript statements into configuration settings. That'll start flagging things under PCI/PA-DSS inspections.

Better to follow something like the existing on_load stuff already in core ... esp since it's essentially the same task, but done differently.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

10 Jul 2011, 7:31 PM
#5
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

Great, thanks for the advice and direction. I had veered away from that but still didn't hit on that.

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

10 Jul 2011, 7:54 PM
#6
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

One of the things I was trying to do is be able to activate/deactivate this by not having the ready doc statement included. So I can definitely have the statement added in by creating a jscript file for the pages/product_info folder, but that's too automatic. Is there a way to flag it off and on 'cause I don't think there is.

Normally the js and the document ready function are separate with jQuery - of course, I'm no expert at jQuery. I just know this is the way the jQuery is done normally.

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

12 Jul 2011, 1:01 AM
#7
lankeeyankee avatar

lankeeyankee

Totally Zenned

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

Re: Including jquery with ready doc statements

Could this benefit from a template based autoloader that has a define for which pages a certain amount of js and css scripts load? I am not sure I understand the issue, so I might be off-base with my suggestion. But we use multiple jquery scripts that are called depending on which page is called and none of them conflict with each other.


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

12 Jul 2011, 11:40 AM
#8
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

lankeeyankee:

Could this benefit from a template based autoloader that has a define for which pages a certain amount of js and css scripts load? I am not sure I understand the issue, so I might be off-base with my suggestion. But we use multiple jquery scripts that are called depending on which page is called and none of them conflict with each other.

This isn't about conflicts - but about using jquery files with document ready statements. This code goes in the header (sometimes in the footer):

<script type="text/javascript">
$(document).ready(function() {
		var origsrc = $("#productMainImage a img").attr("src");
	........

.......................
	  }
	);
});
</script>

Not uncommon to run 2 jquery scripts at the same time, so you add in the other inside the $(document).ready(function() { statement.

What I"m trying to do is to automate that so that no one has to figure out how to add that in or change the html_header. Kinda like adding in another css file but would be done through activation in admin.

This gives the user more control as well since those ready doc statements can do things like change the speed or color of something in the jquery.

This is the norm for jquery. Of course all of that can be done within the jquery file itself but if I can make this work then almost any jquery mod can be easily plugged into Zen Cart. That opens up a lot of possibilities for the community.

Just trying to make it more user friendly since I seem to be creating more and more of these kind of plugins. Selfishly, this would also reduce the amount of support necessary for such plugins!

Also, sometimes a jquery will be needed on every page - don't forget that. It's not about which page to include it on. But how to add in another one later.

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

12 Jul 2011, 4:07 PM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Including jquery with ready doc statements

I repeat ... that's the same concept that the built-in on_load support uses. So, I recommend that you use that as inspiration for your jquery need.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

13 Jul 2011, 11:51 AM
#10
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

Can you give a hint where to start? "built-in on_load support" Obviously, I don't know what you are talking about. I know files, not general terms, and I used zen cart to learn php! Not a strong coder background.

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

13 Jul 2011, 12:37 PM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Including jquery with ready doc statements

Just like there's built-in support for plugin javascript, there's similar support for related onload events:
http://www.zen-cart.com/wiki/index.php/Customisation_-_Templates_-_Javascript#On_Load_Override_How-To

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

13 Jul 2011, 2:27 PM
#12
lankeeyankee avatar

lankeeyankee

Totally Zenned

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

Re: Including jquery with ready doc statements

There's so much to ZC that it's very easy to learn something (or two or ten) new every day with this awesome cart. Thanks for that link, I never knew that it had that capability!


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

13 Jul 2011, 3:13 PM
#13
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

I do love Zen Cart for that. I don't want to become the "expert" who knows every single detail. That would be boring! Learning is more fun.

So I see how that works but it does start with the index.php file. I was hoping to do this without altering core files. I certainly haven't come up with another option though.

Can you think of any way to do without altering core files?

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

13 Jul 2011, 3:22 PM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Including jquery with ready doc statements

Create your own autoloader config file which loads your own custom init_readydoc file which builds the variables you want to output in your template and then echo them in whichever template file you want them to appear.

The "building" within the init file could be as simple as conglomerating several readydoc text files stuffed in a custom template folder's subfolder just like the onload does now. Or maybe a yaml file or something similarly contemporary.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

13 Jul 2011, 3:25 PM
#15
delia avatar

delia

Totally Zenned

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

Re: Including jquery with ready doc statements

oh, thanks so much! That gives me something to get my teeth into!

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