|
|
Thread Tools | Display Modes |
|
#1
|
|
|
Technology Tamer
|
***************
MOD Edit: Adding in the Dummy's way of doing this - same title for EVERY PAGE: put this in your HEAD TAG section of your customization: PHP Code:
A little while ago, I wrote, wondering how to do a number of things. Well, today, I share the fruits of my labor with you. The story is that I was navigating my site, touching up a keyword here and a keyword there on a photo in this gallery and a photo in that gallery, and after switching pages a bunch of times, I hit the back button to look at where I had been... "Mike Fried's Photography, Mike Fried's Photography, Mike Fried's Photography, etc". I had a new mission. Whereas before I had learned the Zen of changing the title in a single line of JavaScript, now I needed to master the Zen of changing the title with some DHTML (Dynamic HTML) to make it appropriate to the page the user was viewing. So I grabbed a web search, and looked around at the source to my site in my handy second web browser (I use Firefox and IE -- IE was logged into my site so I could work on the customization and I reload the site logged out in Firefox at the same time to view the results and search through source pages for interesting things to work with). After about a half hour of searching, learning, and tweaking, I had achieved my goal. Here's how you make the title follow your current page: 1) In the co-branding section of your site, insert the following code into the JavaScript (Replace My Site's Title with something more appropriate): * Note: This has been updated to attempt to address some issues below in this thread. Code:
document.title = "My Site's Title";
function RelevantTitle()
{
var baseTitle = "My Site's Title";
var separator = " - ";
var albumTitle = document.getElementById("albumTitle");
var galleryTitle = document.getElementById("galleryTitle");
if( albumTitle && albumTitle.textContent )
document.title = baseTitle + separator + albumTitle.textContent;
else if( galleryTitle && galleryTitle.textContent )
{
var galleryTitleText = galleryTitle.textContent;
// Strip " sub-categories" off the end of the category text
var finalPositionCategory = galleryTitleText.search(" sub-categories");
if( finalPositionCategory >= 0 )
galleryTitleText = galleryTitleText.substr( 0, finalPositionCategory );
else
{
// Strip " galleries" off the end of the category/sub-category text
var finalPositionSubCategory = galleryTitleText.search(" galleries");
if( finalPositionSubCategory >= 0 )
galleryTitleText = galleryTitleText.substr( 0, finalPositionSubCategory );
}
document.title = baseTitle + separator + galleryTitleText;
}
else // Not Gallery, Category, or Subcategory
{
// Set title on homepage
document.title = baseTitle;
}
}
Code:
<body onload="RelevantTitle();"> Now if only I didn't have to spend an hour re-classifying all my photos last night because I only just figured out how to get a space in a keyword (use double quotes), I would have uploaded more than one new gallery from my backlog of about 60GB of photos and almost 6 years... I feel like I should install the C# SDK on my Media Center PC this weekend and write some individual photo keyword entering code for send to smugmug... -Mike "I like to tinker with my new smugmug account" Fried Last edited by Andy; Jan-29-2006 at 04:07 PM. Reason: Cleaned up the script a little, hopefully fixed the issue Andy discovered in a clean way. |
|
|
|
|
#2
|
|
|
panasonikon
|
Oops - Firefox...
Very cool though :) |
|
|
|
|
#3
|
|
|
panasonikon
|
Safari, looks good
|
|
|
|
|
#4
|
||
|
Technology Tamer
|
Quote:
I get this on 1.5 for PC: |
|
|
|
||
|
#5
|
||
|
Technology Tamer
|
Quote:
Thanks for the screenshots. |
|
|
|
||
|
#6
|
||
|
panasonikon
|
Quote:
|
|
|
|
||
|
#7
|
|
|
panasonikon
|
BTW, your Galapogos trip (VERY COOL PICS) is screaming for this sort of dealie:
http://jennyc.smugmug.com/gallery/1003113 http://friedfamilyphoto.smugmug.com/Vacation/141950 |
|
|
|
|
#8
|
|
|
Picture Reality
|
Great idea, MikeFried! I've taken the liberty of doing a little debugging on your script. Fixed the FF1.5 issue. Here's my version:
Code:
function RelevantTitle() {
var separator = " : ";
var smugmugSuffix = " - powered by smugmug";
var insertAt = document.title.indexOf(smugmugSuffix);
var newTitle = document.title;
var albumTitle = document.getElementById("albumTitle");
var galleryTitle = document.getElementById("galleryTitle");
if(albumTitle && albumTitle.innerHTML) {
var newText = separator + Trim(albumTitle.innerHTML);
newTitle = InsertString(newTitle, newText, insertAt);
}
else if(galleryTitle && galleryTitle.innerHTML) {
var galleryTitleText = Trim(galleryTitle.innerHTML);
var index = galleryTitleText.search(" sub-categories");
if(index > -1) {
// Strip " sub-categories" off the end of the text
galleryTitleText = galleryTitleText.substr(0, index);
} else {
// Strip " galleries" off the end of the text
index = galleryTitleText.search(" galleries");
if(index > -1) {
galleryTitleText = galleryTitleText.substr(0, index);
}
}
newText = separator + galleryTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
document.title = newTitle;
}
function InsertString(text, insertText, index) {
var newText = text.substring(0, index) + insertText + text.substring(index);
return newText;
}
function Trim(text) {
var regexp = /\s+/g;
text = text.replace(regexp, "");
return text;
}
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|
|
|
|
#9
|
|||
|
Technology Tamer
|
Quote:
Quote:
Thanks, Mike |
||
|
|
|||
|
#10
|
|||
|
Picture Reality
|
Quote:
Code:
function Trim(text) {
var regexp = /^\s+|\s+$/g;
text = text.replace(regexp, "");
return text;
}
Quote:
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] Last edited by Gary Glass; Jan-12-2006 at 01:46 PM. |
||
|
|
|||
|
#11
|
||
|
Technology Tamer
|
Quote:
-Mike |
|
|
|
||
|
#12
|
||
|
Picture Reality
|
Quote:
As regex goes, this one is really simple. Using textContent might work just as well. It depends on how textContent handles leading and trailing whitespace. What I was think happening is that there was a linebreak and other whitespace within the tag, which was screwing up the titlebar in FF. So you'll want to test your textContent idea against that scenario. Let me know how it works! In any case, the Trim method is handy tool to keep in your javascript toolbox. (It'd be even more elegant as an extension to the string prototype.)
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|
|
|
||
|
#13
|
|
|
Big grins
|
Hi Gang - this code is awesome, and almost works! I found one little bug, but I'm not a Javascript expert to fix it... When I click down into a gallery, then click on a photo so that the page only shows that one photo, the title bar gets filled with HTML code. See this link for an example:
http://www.michaellandry.com/gallery...39508474/Large Here is the code as it's currently running in my Control Panel: Code:
function RelevantTitle() {
var separator = " : ";
var smugmugSuffix = " - powered by smugmug";
var insertAt = document.title.indexOf(smugmugSuffix);
var newTitle = document.title;
var albumTitle = document.getElementById("albumTitle");
var galleryTitle = document.getElementById("galleryTitle");
if(albumTitle && albumTitle.innerHTML) {
var newText = separator + Trim(albumTitle.innerHTML);
newTitle = InsertString(newTitle, newText, insertAt);
}
else if(galleryTitle && galleryTitle.innerHTML) {
var galleryTitleText = Trim(galleryTitle.innerHTML);
var index = galleryTitleText.search(" sub-categories");
if(index > -1) {
// Strip " sub-categories" off the end of the text
galleryTitleText = galleryTitleText.substr(0, index);
} else {
// Strip " galleries" off the end of the text
index = galleryTitleText.search(" galleries");
if(index > -1) {
galleryTitleText = galleryTitleText.substr(0, index);
}
}
newText = separator + galleryTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
document.title = newTitle;
}
function InsertString(text, insertText, index) {
var newText = text.substring(0, index) + insertText + text.substring(index);
return newText;
}
function Trim(text) {
var regexp = /^\s+|\s+$/g;
text = text.replace(regexp, "");
return text;
}
Michael
__________________
--- Michael Landry Photography, LLC http://www.michaellandry.com photos@michaellandry.com |
|
|
|
|
#14
|
|
|
Picture Reality
|
Thanks for the bug check! I took the opportunity to make some more substantive changes to the code. I didn't like what it did on the home page, so I took it out. The home page now gets its default title. Photo pages show the gallery title followed by the photo title. Here's the new version:
Code:
function ContextualizeTitle() {
var separator = " : ";
var smugmugSuffix = " - powered by smugmug";
var insertAt = document.title.indexOf(smugmugSuffix);
var newTitle = document.title;
var newText = null;
if(!IsHomePage()) {
// add photo title
var photoTitleText = GetPhotoCaption();
if(photoTitleText.length > 0) {
newText = separator + photoTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
// add album title
var albumTitle = document.getElementById("albumTitle");
if(albumTitle) {
var albumTitleText = GetTextContent(albumTitle);
if(albumTitleText.length > 0) {
newText = separator + albumTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
} else {
// add gallery title
var galleryTitle = document.getElementById("galleryTitle");
if(galleryTitle) {
var galleryTitleText = GetTextContent(galleryTitle);
if(galleryTitleText.length > 0) {
var index = galleryTitleText.indexOf(" galleries");
if(index > -1) {
galleryTitleText = galleryTitleText.substr(0, index);
}
newText = separator + galleryTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
}
}
document.title = newTitle;
}
}
function GetPhotoCaption() {
var caption = "";
var photoTitle = document.getElementById("caption_bottom");
if(!photoTitle) {
photoTitle = document.getElementById("caption_top");
}
if(photoTitle) {
caption = GetTextContent(photoTitle);
}
return caption;
}
function GetTextContent(node) {
var text = "";
if(node) {
if(node.children) {
text = GetTextContent(node.firstChild);
} else {
if(node.nodeValue) {
text = node.nodeValue; // IE
} else {
text = node.textContent; // mozilla
}
}
}
text = Trim(text);
return text;
}
function InsertString(text, insertText, index) {
var newText = text.substring(0, index) + insertText + text.substring(index);
return newText;
}
function Trim(text) {
var regexp = /^\s+|\s+$/g;
text = text.replace(regexp, "");
return text;
}
function IsHomePage() {
var isHomePage = false;
// test for the "homepage" class name in the <BODY> tag
var classStr = document.body.className;
if (classStr && (classStr.indexOf("homepage") != -1)) {
isHomePage = true;
}
return isHomePage;
}
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|
|
|
|
#15
|
||
|
Technology Tamer
|
Some things are broken...
Quote:
Anyway, I've incorporated your recent changes, fixed these issues, tested with IE and FF, and changed my coding style slightly. I'm curious how we might add keywords, maybe by parsing the document's URL, but that will have to wait for another day... Attached is part of the code from my site, made more suitable for a sample (I've given up on this code formatted in the text editor thing). -Mike |
|
|
|
||
|
#16
|
||||||||
|
Picture Reality
|
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|||||||
|
|
||||||||
|
#17
|
|
|
Picture Reality
|
FYI. I was finally able to push the latest to my site, if you want to see what it does.
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|
|
|
|
#18
|
||
|
Call me Andrew
|
Quote:
|
|
|
|
||
|
#19
|
||
|
SmugMug Sorcerer
|
Quote:
firefox 1.5 - logged in firefox 1.5 - logged out IE 6 - logged in IE 6 - logged out all on winxp sp2
__________________
Pedal faster |
|
|
|
||
|
#20
|
|
|
Picture Reality
|
Dang! Why you making me work so hard! Hey, do me a favor, post the URLs of those pages, and I'll check into it. Thanks, Big Guy!
__________________
[FONT="Garamond"]Gary Glass[/FONT] [FONT="Tahoma"]ShutterGlass.com[/FONT] [FONT="Tahoma"]OnlyBegotten.com[/FONT] |
|
|
|
| Tell The World! | |
| Thread Tools | |
| Display Modes | |
|
|