NC's way of JQuery implementation is anoying...

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
I have a big request and that is to add the jQuery to the head section of the master pages.
And I asked this because the way NC has implemented jQuery through .cs back end code is annoying, irritating and blocking me considerably in my customization and creativity as front end designer.


The problems using jQuery in the way it is now:

One:
A big problem with NC's way of binding jQuery in the source is that sometimes the jQuery gets loaded/placed after the jQuery plugin script when the page is build. Thats not a good thing and gives JS errors. The scripts belongs in the head section.

Two:
If you want to add (another) custom jQuery script you need to rebuild again.

Three:
Currently the jQuery is binded on the search and product page in the shop. If you want to use jQuery on all/other pages you have to add the jQuery bind in the XX.CS of every page that is used in the shop and rebuild.

Four: If you want to add a jQuery plugin you need to add it to the source and need to rebuild.


Better implementation of jQuery:

Place code in master pages head section.


<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
<![CDATA[
  google.load("jquery", "1.4.2"); // jquery codebase
  // google.load("jqueryui", "1.7.2"); // basic ui functions
  // google.load("swfobject", "2.2"); // flashplayer object
]]>
</script>

<!--
ADD YOUR CUSTOM jQuery plugins/scripts here if needed
-->

</head>



Advances:
1) This is easy to manage without even the need to rebuild project.
2) No need to add a Bind to jQuery in source.
3) Saves bandwidth because our friends at Google delivering the jQuery main code.
4) Because of google script block browser caches the load of the script.
5) Add plugins or custom script without the need to rebuild.
6) ease of use, saves time.
7) Can use jQuery on any page without the need of CS code and re-building.
8) Added the scripts to the head section where it belongs.



To make up for the time being add the following to BaseNopUserControl.cs:


In  BaseNopUserControl.cs


protected virtual void BindJQuery()
        {
            //string jquery =  CommonHelper.GetStoreLocation() + "Scripts/jquery-1.4.min.js";

            string jqueryBase = "http://www.google.com/jsapi";
            StringBuilder jblock = new StringBuilder("<script>");
          
            jblock.AppendLine("google.load(\"jquery\", \"1.4.2\");");

           // ## IF Needed:
            // jblock.AppendLine(" google.load(\"jqueryui\", \"1.7.2\");");
            // jblock.AppendLine(" google.load(\"swfobject\", \"2.2\");");            

           jblock.AppendLine("</script>");

            Page.ClientScript.RegisterClientScriptInclude(jqueryBase, jqueryBase);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jblock.ToString(), jblock.ToString());
                    
          
            //### include custom plugin scripts:
            // string jcorners =  CommonHelper.GetStoreLocation() + "Scripts/jcorners.js";
            // Page.ClientScript.RegisterClientScriptInclude(jcorners, jcorners);
            // string slideshow =  CommonHelper.GetStoreLocation() + "Scripts/slideshow.js";
            // Page.ClientScript.RegisterClientScriptInclude(slideshow, slideshow);
            // string jdefault =  CommonHelper.GetStoreLocation() + "Scripts/jdefault.js";
            // Page.ClientScript.RegisterClientScriptInclude(jdefault, jdefault);

  

        }


In BaseNopPage.cs:


protected override void OnPreRender(EventArgs e)
        {
            string publicJS = CommonHelper.GetStoreLocation() + "Scripts/public.js";
            Page.ClientScript.RegisterClientScriptInclude(publicJS, publicJS);

             string jqueryBase = "http://www.google.com/jsapi";

            StringBuilder jblock = new StringBuilder("<script>");
             jblock.AppendLine("google.load(\"jquery\", \"1.4.2\");");

           // ## IF Needed:
            // jblock.AppendLine(" google.load(\"jqueryui\", \"1.7.2\");");
            // jblock.AppendLine(" google.load(\"swfobject\", \"2.2\");");            

           jblock.AppendLine("</script>");
          
            Page.ClientScript.RegisterClientScriptInclude(jqueryBase, jqueryBase);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jblock.ToString(), jblock.ToString());
          
            //### include custom plugin scripts:
            //string jcorners = CommonHelper.GetStoreLocation() + "Scripts/jcorners.js";
            //Page.ClientScript.RegisterClientScriptInclude(jcorners, jcorners);
            //string slideshow = CommonHelper.GetStoreLocation() + "Scripts/slideshow.js";
           //Page.ClientScript.RegisterClientScriptInclude(slideshow, slideshow);
           //string jdefault = CommonHelper.GetStoreLocation() + "Scripts/jdefault.js";
           //Page.ClientScript.RegisterClientScriptInclude(jdefault, jdefault);
          
     base.OnPreRender(e);
        }

13 years ago
Thanks for suggestion. I've created a work item for this task
13 years ago
USRFobiwan,

While I don't disagree with your assessment that nopCommerce makes jQuery harder to use than it should be, might I bring up one point?

It has been suggested (at Yahoo for example: http://developer.yahoo.com/performance/rules.html) that placing scripts towards the bottom of the page may help increase page load time.

I myself have always been a "scripts in the header" kind of guy, but if it will make the user experience better and not impact the way my site operates then I'm all for putting the scripts at or near the bottom.

What is your opinion on this?

B
13 years ago
I do agree with suggestion of USRFobiwan, its difficult to work with jquery in current situation and many other consequences already mentioned in original post.

The RegisterClientScriptInclude() method (currently used for jquery binding in nopcommerce) adds a script block at the top of the rendered page so there are no such performance gains.

RegisterStartupScript () method will place script block at bottom, right above the closing form tag as its executes when page finished loading so use of this method can do the trick but also in this case changes needs to be done for jquery binding.

A last thing, scripts are cached by browser for first time use so there is no need to add extra complex way of adding jquery, which needs recompilation of site if script source changes.

Let me know if have mistaken something.
13 years ago
There are two possible options (three if you count inline scripts) including an external script file via a script tag in the head section or including and external (3th party) script at the bottom before the </body></html>.

Although placing scripts at the bottom let the page load normal before the engine hits the script and loads it as the Yahoo research showed on some browsers. I believe loading external scripts (as in not coming from your site) like 'widgets', 'blog' or things like 'Share it' scripts can be useful if for example the external site is down and your page times out on these scripts.

However, if your script has to activate or interact with things on the page before it is showing it to the user. You want the script loaded as early as possible, or which must execute as soon as the DOM is ready, you may need to have it in the head. Another issue is layout - if your script is going to change the page layout, like jQuery does, you want it loaded as early as possible so your page does not spend a long time redrawing itself in front of your users.

The yahoo research may be true at one time, but IMHO, with all the new stuff going on recently, Just look at the browser wars on which browser can deliver the fastest JavaScript engine and do not forget the bandwidth people now have, you talking milliseconds load time. The largest JavaScript file is still smaller then the individual images uses on most sites.


So basically the below setup would be a good starting point:

1) Put scripts in head that needs to interact with page layout (like jquery)
2) Put non interacting script and external script into bottom (linkedIn, Share etc)
3) Optimize your script, and use a packer


JQuery loading from own server?
There is no benefit to this method over hosting JQuery by yourself. The main reason to use Google's JQuery is that people have it cached already in their browser, so no download at all is required. In this case, the browser would have to cache your server-side script, since it's coming from your domain. However it is save to build in a fall-back, if Google gets down or for countries where Google is banned.

Fall-back option:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
// has the google object loaded?
if (window.google && window.google.load) {
    google.load("jquery", "1.4.2");
} else {
    document.write('<script type="text/javascript" src="YOURSITE/Scripts/jquery-1.4.2.min.js"><\/script>');
}
</script>



Latest version loading, if you are a purist you can always load the latest version of JQuery, although for production I would not recommend this.

google.load("jquery", "1");



And for the anti Google fanatics, you can just use:
<script type="text/javascript" src="/Scripts/jquery-1.4.2.min.js"></script>


The main point is, that with putting it in the master pages and not in code behind. It is much easier to maintain or change, you do not need to rebuild your site and upload it. You can change it inside the master pages, save it and thats it.
13 years ago
I'm having an extremely hard time implementing JQuery, can someone please write out a step by step ffor a newbie like me ?
13 years ago
Im a n00b too, 3 questions

1.
Has this been implemented in v1.90 ?

2.
If not, will the code in the poster still work? or do I need to delete some code somewhere else?
13 years ago
On my first try at adding JQuery I just put the loading in the head of the Root.Master so -


<script src="Scripts/jquery-1.4.min.js" type="text/javascript"></script>
<script src="Scripts/cufon-yui.js" type="text/javascript"></script>
<script src="Scripts/cufon-replace.js" type="text/javascript"></script>
<script src="Scripts/Jikharev_400.font.js" type="text/javascript"></script>
</head>


This worked fine on the first level pages except the forum. It does not work on some of the inner pages like the various category pages. I can't seem to suss this out. I may try the workaround above.
13 years ago
In 1.90 it is implemented like the example above..and a little smarter
13 years ago
I figured out I had a problem with the path in my script. However, different ways of writing the path work for some sections of the website and not for others.

like this:

<script src="../Scripts/jquery-1.4.min.js" type="text/javascript"></script>


It does not resolve on the home page, or any first level page but will resolve for the Forum or Category pages.

like this:

<script src="./Scripts/jquery-1.4.min.js" type="text/javascript"></script>
<script src="./Scripts/cufon-yui.js" type="text/javascript"></script>
<script src="./Scripts/cufon-replace.js" type="text/javascript"></script>
<script src="./Scripts/Jikharev_400.font.js" type="text/javascript"></script>


Will resolve on the first level pages but not deeper in the directory like the Forum and Category pages.

It makes sense in a way but there is no simple workaround....
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.