<title> and <meta> tags - again

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Tue Feb 17, 2009 8:14 am

I apologize. I meant where in the files would this need to be placed. I assume that it must over-write a part of the existing code in the page?
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Sun Feb 22, 2009 2:10 pm

I am still at a bit of a loss. When I add the code to a file and then view the source, there appears to be no change.
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: <title> and <meta> tags - again

Post by mazhar » Mon Feb 23, 2009 7:17 am

In fact the purpose of that code was to adjust the outputted HTML. Problem was to put line breaks between meta tags and title tag and arrange them in desired order. I redirected you to this code from another thread because this code is providing you the place to alter the head info. So as for as I remember about your problem that you want to either remove or adjust the title tag. You can change the code to meet your needs.

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Mon Feb 23, 2009 8:04 am

You are correct, Mazhar. I had wanted to remove the title tag. However, with reading the thread I felt that it may also benefit us to incorporate the entire code (altering after as needed) in order to clean up the HTML output. When I try to use the code above and compare the results by going to the page and viewing the source, I see no change. Am I possibly placing the code into the file (for example, Default.aspx) in the wrong location? Is it supposed to overwrite something, as I did not overwrite anything. I placed the code inside the <asp> tags, as anywhere else seemed to result in an error.
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: <title> and <meta> tags - again

Post by mazhar » Mon Feb 23, 2009 10:20 am

For default page you will use it something like

Code: Select all

<%@ Page Language="C#" MasterPageFile="~/Layouts/Scriptlet.master" Inherits="CommerceBuilder.Web.UI.AbleCommercePage" Title="Home Page" %>
<%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls.WebParts" TagPrefix="cb" %>

<script runat="server">
    protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>", titleText);
                    sb.Replace(titleTag, titleText);
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }
</script>

<asp:Content runat="server" ContentPlaceHolderID="PageContent">
    <cb:ScriptletPart ID="HomePage" runat="server" Layout="Three Column" Content="Home Page" Sidebar="Standard Sidebar 1" Sidebar2="Standard Sidebar 2" Header="Standard Header" Footer="Standard Footer" Title="Home Page" AllowClose="False" AllowMinimize="false" />
</asp:Content>


Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Mon Feb 23, 2009 7:18 pm

I find that using the altered code, which is intended to move the title tag to the beginning of the meta list, we end up with two title tags. Is there a way to fix this?

Also, some suggest that the title and meta tags should be at the very start of the page code. Is there a way to accomplish this, as currently it is lower in the code? I would think that today's search engines are smart enough to ignore the exact placement of this portion of the code (and to that end, on certain engines we are first page for all terms that we have optimized for, and that without any alteration of the code, so I assume it is not as important as it once was).
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: <title> and <meta> tags - again

Post by mazhar » Tue Feb 24, 2009 7:40 am

Brewhaus wrote:I find that using the altered code, which is intended to move the title tag to the beginning of the meta list, we end up with two title tags. Is there a way to fix this?

Also, some suggest that the title and meta tags should be at the very start of the page code. Is there a way to accomplish this, as currently it is lower in the code? I would think that today's search engines are smart enough to ignore the exact placement of this portion of the code (and to that end, on certain engines we are first page for all terms that we have optimized for, and that without any alteration of the code, so I assume it is not as important as it once was).

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>\r", titleText);

                    int firstMetaIndex = sb.ToString().IndexOf("<META", 0, sb.Length, StringComparison.InvariantCultureIgnoreCase);
                    if (firstMetaIndex > -1)
                    {
                        sb.Replace(titleTag, string.Empty);
                        sb.Insert(firstMetaIndex, titleText);
                    }
                    else
                    {
                        sb.Replace(titleTag, titleText);
                    }
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Tue Feb 24, 2009 8:06 am

That works perfectly- thank you, Mazhar!
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: <title> and <meta> tags - again

Post by Mike718NY » Mon Mar 02, 2009 9:49 am

Hey Rick,
I was checking out your <title> and meta tags and I noticed you have
duplicate <title> tags for the one category:
..........
<title>Hot Sauce Depot- BBQ Sauce</title>
<title>BBQ Sauces from Hot Sauce Depot</title>
<style type="text/css">...................

A few of the other categories I checked just have one, but this one has 2 ??

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Mon Mar 02, 2009 11:08 am

Thank you for catching this and letting us know- we have fixed it. :)
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: <title> and <meta> tags - again

Post by Mike718NY » Mon Mar 02, 2009 3:33 pm

Rick,
Could you please tell me the code you used to get your <title> and <meta> tags
on your Default.aspx home page? thanks...............

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: <title> and <meta> tags - again

Post by Mike718NY » Mon Mar 02, 2009 4:43 pm

I was trying to do this in code, but forgot about the Admin
Website > Content and Layout section where you can do it.

But strange behaviour. If I put that google verification thing, <meta name="verify-v1" ....
before the first <meta . . tag, the <title> appears first, before the <meta>'s,
otherwise it's below them:

<title>Default.aspx page</title>

<meta name="verify-v1" content="BLLdFTifcnOYvlkM=" />
<meta name="description" content="Discount, etc, PayPal Available." />
<meta name="keywords" content="Supplements, Paypal" />
<title>
Home Page
</title>

But, . . I have a duplicate <title> tag now. Is there anyway of getting rid of that?

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: <title> and <meta> tags - again

Post by Brewhaus » Mon Mar 02, 2009 7:28 pm

I physically changed the title in the Default.aspx file, and added the meta tags in the Home Page scriptlet. That was simple and seemed to do the trick.
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Sun Jun 21, 2009 10:17 am

The code enhancements works but the title tags was inserted in the webpart.css link reference code (see below in red).
Any thought on how to fix this AND move the title below the <html> code block?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="ctl00_head1">
<script language="javascript">
function initAjaxProgress()
{
var pageHeight = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
//SET HEIGHT OF BACKGROUND
var bg = document.getElementById('ajaxProgressBg');
bg.style.height = (pageHeight + 1000) + 'px';
//POSITION THE PROGRESS INDICATOR ON INITIAL LOAD
reposAjaxProgress();
//REPOSITION THE PROGRESS INDICATOR ON SCROLL
window.onscroll = reposAjaxProgress;
}

function reposAjaxProgress()
{
var div = document.getElementById('ajaxProgress');
var st = document.body.scrollTop;
if (st == 0) {
if (window.pageYOffset) st = window.pageYOffset;
else st = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
}
div.style.top = 150 + st + "px";
}
</script>

<link href="App_Themes/airshowshop/ComponentArt.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/airshowshop/print.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/airshowshop/style.css" type="text/css" rel="stylesheet" />
<link href="App_Themes/airshowshop/webparts.css" type="text/css" rel="stylesheet" /
<title>DVD</title>>
<meta name="keywords" content="Blue Angels"/>
<style type="text/css">
.ctl00_ContentZone_0 { border-color:Black;border-width:1px;border-style:Solid; }

</style>
</head>
Thank you,
Gio

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: <title> and <meta> tags - again

Post by mazhar » Mon Jun 22, 2009 5:08 am

Its working fine for me. I tried following code

Code: Select all

protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>\r", titleText);

                    int firstMetaIndex = sb.ToString().IndexOf("<META", 0, sb.Length, StringComparison.InvariantCultureIgnoreCase);
                    if (firstMetaIndex > -1)
                    {
                        sb.Replace(titleTag, string.Empty);
                        sb.Insert(firstMetaIndex, titleText);
                    }
                    else
                    {
                        sb.Replace(titleTag, titleText);
                    }
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Mon Jun 22, 2009 6:06 am

@Mazhar - If I place that code in default.aspx and product.aspx it works fine. Without creating duplicate code all over the place I thought the purpose was to place the code above in the pagehelper file and use the render snippet (below) for default.aspx, product.aspx. etc. When I do that I get the error highlighted in red from my previous post.

##### render snippet for pagehelper.cs ################
protected override void Render(HtmlTextWriter writer)
{
string pageHtml = string.Empty;

using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
base.Render(hw);
pageHtml = sw.ToString();
PageHelper.AdjustHTML(ref pageHtml);
hw.Close();
}
sw.Close();
}
writer.Write(pageHtml);
}
Thank you,
Gio

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Re: <title> and <meta> tags - again

Post by Logan Rhodehamel » Mon Jun 22, 2009 11:23 am

Hello All-

I would like to offer a tip... what could be done here to simplify things. Instead of duplicating the code into each file, I think you all could be using a custom base page class.

In the top of these files, you see code like

Code: Select all

Inherits="CommerceBuilder.Web.UI.AbleCommercePage
But what you all want to do is override the Render method and share that across multiple pages. It might be simpler to create a new class inherited from AbleCommercePage. Then instead of duplicating lots of messy code, you can just update the Inherits statement for files you want to adjust.

That probably does not make a lot of sense for non-developers, so I will follow up this post with some sample code.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Re: <title> and <meta> tags - again

Post by Logan Rhodehamel » Mon Jun 22, 2009 11:43 am

First, create a file called App_Code/CustomBasePage.cs and put this inside:

Code: Select all

using System;
using System.IO;
using System.Text;
using System.Web.UI;

/// <summary>
/// Adjusts the header output of the base page for improved SEO
/// </summary>
public class CustomBasePage : CommerceBuilder.Web.UI.AbleCommercePage
{
	public CustomBasePage() { }

    protected override void Render(HtmlTextWriter writer)
    {
        string pageHtml = string.Empty;
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                base.Render(hw);
                pageHtml = sw.ToString();

                if (pageHtml.Contains("<head") && pageHtml.Contains("</head>"))
                {
                    int headStartIndex = pageHtml.IndexOf("<head");
                    int headEndIndex = pageHtml.IndexOf("</head>");

                    string headerPart = pageHtml.Substring(headStartIndex, ((headEndIndex + 7) - headStartIndex));

                    StringBuilder sb = new StringBuilder(headerPart);
                    sb.Replace("<link", "\r<link");
                    sb.Replace("<META", "\r<META");
                    sb.Replace("<style", "\r<style");

                    int sindex = headerPart.IndexOf("<title>");
                    int eindex = headerPart.IndexOf("</title>");
                    string titleTag = headerPart.Substring(sindex, (eindex + 8) - sindex);
                    string titleText = headerPart.Substring((sindex + 7), (eindex - (sindex + 7)));
                    titleText = titleText.Replace("\r", string.Empty);
                    titleText = titleText.Replace("\t", string.Empty);
                    titleText = titleText.Replace("\n", string.Empty);
                    titleText.Trim();
                    titleText = String.Format("\r<title>{0}</title>\r", titleText);

                    int firstMetaIndex = sb.ToString().IndexOf("<META", 0, sb.Length, StringComparison.InvariantCultureIgnoreCase);
                    if (firstMetaIndex > -1)
                    {
                        sb.Replace(titleTag, string.Empty);
                        sb.Insert(firstMetaIndex, titleText);
                    }
                    else
                    {
                        sb.Replace(titleTag, titleText);
                    }
                    sb.Replace("</head>", "\r</head>");
                    string newHeaderPart = sb.ToString();
                    pageHtml = pageHtml.Replace(headerPart, newHeaderPart);
                }
                hw.Close();
            }
            sw.Close();
        }
        writer.Write(pageHtml);
    }
}
Second, for any page you wish to use the modified render you need to adjust the first line of the file. For instance to make Default.aspx use the modified page class, edit the file and change the first line:

Code: Select all

<%@ Page Language="C#" MasterPageFile="~/Layouts/Scriptlet.master" Inherits="CustomBasePage" Title="Home Page" %>
All that needs to change is the Inherits= part. Now any improvements to the header rendering will only need to be made in a single location.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Mon Jun 22, 2009 12:25 pm

@Logan_AbleCommerce - works perfectly. It definitely should be part of your next AC build.

With the category.aspx page I hacked the code so I could modify the title tag (see snippet below). This was done because modifying the title in the page declaration doesn't work. Any thoughts?

....
titleText = String.Format("\r<title>{0} | Airshow Shop</title>\r", titleText);
....

Thanks,
Gio
Thank you,
Gio

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Mon Jun 22, 2009 12:33 pm

@Logan_AbleCommerce - There may be a glitch with how its used with products.aspx code. The name of the product is no longer populating the title tag and is defaulting to the title tag in the product.aspx file.

Gio
Thank you,
Gio

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Re: <title> and <meta> tags - again

Post by Logan Rhodehamel » Mon Jun 22, 2009 12:43 pm

gio50000 wrote:@Logan_AbleCommerce - There may be a glitch with how its used with products.aspx code. The name of the product is no longer populating the title tag and is defaulting to the title tag in the product.aspx file.
Your modification was page specific so the shared base page class would not handle that customization. I have some ideas on how that could be handled as well. I am going to experiment with meta tags, title tags, and the output in general later this afternoon.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Thu Jun 25, 2009 6:24 am

@Developer - Any progress on the updates we discussed?

Gio
Thank you,
Gio

User avatar
gio50000
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Mon Feb 18, 2008 12:51 pm
Location: Orlando, FL
Contact:

Re: <title> and <meta> tags - again

Post by gio50000 » Sat Oct 24, 2009 2:11 pm

Any ideas on how to use krissato's format suggestion for category and product detail pages?
Thank you,
Gio

Post Reply