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.
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.
Code:
<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?
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?
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.
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.
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.
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.
Re: Including jquery with ready doc statements
Quote:
Originally Posted by
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):
Code:
<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.
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.
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.