blog

January 14, 2019

How to detect a Blogger homepage with jQuery?

This may come in handy:

    Although Blogger has its own way of determining is a currently displayed page a home/welcome page of Your website - there are cases in which it could be handier to use jQuery for this purpose. But if You'd like to do so, one question arises: on what basis such a detection could be done?

    I don't use the same solution for this each time - I rather prefer to pick one out of several of them, depending on the current project's specifics. The reason is that various projects may provide You with various opportunities which You could use within jQuery code to make it work. Let's take a closer look on various examples of such cases:

    Based on the menu.
    One thing worth knowing about menu in Blogger (a PageList gadget usually called #PageList1) is that it can mark its starting item ("home", "start", etc. - a link leading to the homepage) with a .selected class - provided that a currently displayed page is in fact a homepage. If You don't use so-called pages in Blogger - there won't be any more items in menu which could act similar way - this is a little drawback of Blogger menu, although it can be fixed :) . Now what is important to know is that if You don't use Blogger "pages" - the only item which could be marked with a .selected class will be a home/start link (usually the first item in the menu). You can use all this knowledge as a determining factor for jQuery.

    Let's begin with an universal way, determining if there is any item in the menu, with .selected class ascribed?

    Note that I use comments which are incredible useful while working with the code, especially in the long run, when You need to find a particular code snippet within a long-way-no-see project :) . They will always help You to orientate and navigate inside of a code of any volume.

    /* besides Welcome Page */

    if ($("#PageList1").not(":has(li.selected)").length > 0)
    { ...Your code here... }

    /* only on Welcome Page */

    else
    { ...Your code here... }

    Explanation:
    As You can see, it is a simple code based on an if/else concept. If menu has no li elements with the .selected class ascribed - then... (here You put all the instructions which You need to use besides the homepage). Otherwise do something else (here You put all the instructions which You need to use only on the homepage).

    Although some instructions tailored especially for the start page wouldn't bother all the rest of Your code - it is still a good practice to separate code referring to different Blogger page types from each other for the optimization sake (this way unnecessary code won't be triggered without a need - so Your website will use less CPU power of Your Guests' devices).

    If You use, however, Blogger so-called pages and want to be sure that Your mechanism will be working right, You may point out a particular item of the menu as a determining factor - here You are a three different approaches to this:

    /* only on Welcome Page */

    if ( $( "#PageList1 li:contains('xxx)" ).is( ".selected" ) )
    { ...Your code here... }

    Explanation:
    If a menu item which contains "xxx" (replace it with Your name for the home page link - "home", "start", etc.) has a .selected class, then... (here You put all the instructions which You need to use only on the homepage).

    or

    /* only on Welcome Page */

    if ($("li.selected:contains('xxx')").length > 0)
    { ...Your code here... }

    Explanation:
    If there is a menu item which contains "xxx" (replace it with Your name for the home page link - "home", "start", etc.) and has a .selected class, then... (here You put all the instructions which You need to use only on the homepage).

    or the other way round (love the jQuery flexibility :) !):

    /* besides Welcome Page */

    if ($("#PageList1 li:contains('xxx'), #PageList1 select:contains('xxx')").not(":has(a.selected)").length > 0)
    { ...Your code here... }
    else

    /* only on Welcome Page */

    { ...Your code here... }

    Explanation:
    If a menu item which contains "xxx" (replace it with Your name for the home page link - "home", "start", etc.) does not have a .selected class, then... (here You put all the instructions which You need to use besides the homepage). Otherwise do something else (here You put all the instructions which You need to use only on the homepage).

    Note that Blogger can generate a menu either in a list or an option HTML element. The example above illustrates how You can include both of them: li and select as elements of lists of a different type.

    Based on "home page exclusive" :) .
    Another way of detecting the home page in Blogger seems to be pretty obvious and probably the easiest one: just use something which is present only on this (home) page :) . It can be just some particular word, for example:

    /* only on Welcome Page */

    if ($("body").has("#ZZZ h2:contains('xxx')").length > 0)
    { ...Your code here... }

    Explanation:
    If there is a word xxx present somewhere on the currently displayed page, then... (here You put all the instructions which You need to use only on the homepage).

    Be careful of defining Your key word because there are some chances that the same one may be present elsewhere, in some post's text. So to make it safely it is a good idea to precise a location of Your key word - like in the example above, which has to do with a keyword presents only within a header (h2) inside of a #ZZZ element

    Based on posts' headers.
    So far I've described ways to differentiate the home/start page from all the rest kinds of Blogger pages (such as full post page, archive page, label page, and so on). If, however, it may suit You to use some particular code wherever but the full post page - You can do this in a way described below.

    As You may know, Blogger usually turns posts' headers into hyperlinks (leading to their full version). There is only one case in which Blogger does not do this: if such a full version is currently displayed (so there is no need to click a post's header). This way it gives You another opportunity to design a jQuery code snippet - the key question here will be: is there a post header which is not a hyperlink?

    /* only on the full post page */

    if ($(".post-body h3").not(":has(a)").length > 0)
    { ...Your code here... }
    else

    /* elsewhere */

    { ...Your code here... }

    Explanation:
    If a currently displayed (either in full or in an excerpt form) post's header is not a hyperlink, then... (here You put all the instructions which You need to use besides the homepage or only on the full post page). Otherwise do something else (here You put all the instructions which You need to use elsewhere but the full post page).


    There are many more ways of detecting a Blogger homepage - the ones described above are only a fraction of what I personally use because they work fine for Me and I didn't usually need any further solution. But when to think of jQuery potential and combine it with Your imagination - sky's the limit: You may quickly come to conclusion that there indeed may be much more ways of accomplishing this matter. And this is a beautiful part in jQuery (or programming as such) that there are usually many various ways of doing the same thing - which opens the door for Your creativity and stimulates to think out of the box.

    No comments:

    Post a Comment