Search Results

Why a Web page is like a blueberry pie

Yesterday afternoon I Tweeted this: I thought it might be worth explaining what I was talking about, using a metaphor lots of people would understand. So, consider a blueberry pie. To bake a blueberry pie, you assemble your ingredients, do some mixing and combining and stick it in the oven. Let’s say, though, that after [...]

Yesterday afternoon I Tweeted this:

Poking the DOM with PHP. I know YOU all knew about it but *I* just found it and it's wonderful.July 20, 2010 2:01 am via Echofon

I thought it might be worth explaining what I was talking about, using a metaphor lots of people would understand. So, consider a blueberry pie. To bake a blueberry pie, you assemble your ingredients, do some mixing and combining and stick it in the oven. Let’s say, though, that after the pie is out of the oven and cooled, a picky young man with short blond hair says “but Mom, I wanted cherry pie!” Being the outstanding parent you are, you lift the top off the pie, pick out every single blueberry and replace them with cherries. (I know, just go with it.) Remember that, it’ll be important in a few minutes.

On another day, you’re preparing your pie ingredients when the aforementioned picky young man again requests a cherry pie instead of blueberry. This time, you can replace the blueberries with cherries before mixing the ingredients and baking the pie. Same result, but one is clearly easier and less disruptive on the final product.

Finally, the pie is done and you cut a slice. Look at the cross-section of that slice of pie. You have a crust, filling, berries, maybe some ice cream or whipped cream on top. That’s the structure of the completed pie that’s ready to eat.

How does this tasty metaphor apply to Web pages, and specifically to my not-entirely informative Tweet?

A Web page, like a pie, has a structure that can be examined. It has text, images, colors, different fonts, maybe some embedded video clips. These are all elements that make up the structure of the Web page. This structure is called the Document Object Model, or DOM–for purposes of this post it’s not important what that means, but where you see “DOM” you can read “bits and pieces that make up a Web page.”

Let’s say you want to change one of these elements. You could do it after the page has been loaded (the Web browser is showing the page) or you could do it before the page has been sent to the Web browser. Changing the elements of the Web page after it has been loaded is usually done with JavaScript, which is code that runs in the Web browser and is analogous to removing the blueberries after the pie is baked. Changing the elements before the Web page is sent to the browser is, in this instance, done with PHP, which is code that runs on the Web server. This is like adjusting the pie recipe before mixing the ingredients.

The major difference, and why I was excited to discover this capability of PHP, is that adjusting or changing the Web page after it’s been loaded (with JavaScript) is about as simple as removing blueberries from a fully-baked pie. It depends on the capabilities of the Web browser viewing the page and in some cases of the OS running the Web browser. It also adds to the total amount of data sent from the Web server down to the browser (all content [text, images, video, etc] plus the extra code which runs after the page has loaded) and is just generally inefficient.

On the other hand, the ability of PHP to change the contents and structure of a Web page while its still in ingredient form and hasn’t been mixed and baked means that what’s sent down to the Web server is already what I want it to be and I can tweak it as much as I need to before it’s served.

This is a drastic oversimplification, of course–there are plenty of good reasons to fiddle with the DOM using JavaScript once the page has loaded; Web applications like Gmail, Twitter and even WordPress use this capability all the time to change, add or delete elements of the page without making a round trip back to the server. There are also lots of applications where large-scale preprocessing is done on the server with PHP and final tweaks are done in the browser, often depending on the specific browser and version being used. However, the pie metaphor does make a good high-level explanation of this aspect of Web page development.

At least, I think it does. What do you think? Comments welcome.

Comments (1)

And Apache said “let there be redirects” and Lo, there were redirects

I am pretty sure this covers everything from the old Blogger-published site. # root index page Redirect permanent /index.shtml /wp/index.php # Blogger’s year-month archive pages RedirectMatch permanent /([0-9]+)_([0-9]+)_[0-9]+_archive\.shtml /wp/$1/$2 # Blogger’s ‘label’ (tag) pages; labels were not changed to tags # in migration, so redirect to a WP search for that word RedirectMatch permanent /labels/(.*)\.shtml [...]

I am pretty sure this covers everything from the old Blogger-published site.

# root index page
Redirect permanent /index.shtml /wp/index.php

# Blogger's year-month archive pages
RedirectMatch permanent /([0-9]+)_([0-9]+)_[0-9]+_archive\.shtml /wp/$1/$2

# Blogger's 'label' (tag) pages; labels were not changed to tags
# in migration, so redirect to a WP search for that word
RedirectMatch permanent /labels/(.*)\.shtml /wp/index.php?s=$1

# this works on all post pages except where Blogger 'helpfully' omitted words
RedirectMatch permanent /([0-9]+)/([0-9]+)/([0-9a-zA-Z-]+)\.shtml /wp/$1/$2/$3

For those posts where the post-pages published by Blogger omitted words from the title in the filename, the last redirect would result in a 404 (Blogger would tend to omit “a”, “an”, “the” and other small words from titles when creating post-pages). So I’ve installed the Smart 404 plugin (and hacked the code a bit, of course) to auto-suggest a matching post and/or search for keywords.

Suggestions and comments welcome!

Leave a Comment

Welcome to the new and improved Project Insomnia

Aren’t “new” and “improved” mutually exclusive? That always bothered me. Anyway, with Blogger dropping support for ftp publishing I have moved to a self-hosted WordPress instance and migrated all 1,343 posts over. Unfortunately, comments in Haloscan didn’t make it and I’m not sure they can be migrated at all–but I’m still looking at that*. Meanwhile, [...]

Aren’t “new” and “improved” mutually exclusive? That always bothered me. Anyway, with Blogger dropping support for ftp publishing I have moved to a self-hosted WordPress instance and migrated all 1,343 posts over. Unfortunately, comments in Haloscan didn’t make it and I’m not sure they can be migrated at all–but I’m still looking at that*. Meanwhile, WordPress has its own very nice commenting system.

The URL for new posts will be http://www.project-insomnia.com/wp/ for a while, until I do some cleaning and straightening in the root.

Feeds are in your browser’s address bar and there’s an actual, working site-search tool in the upper left corner for your convenience. If you see anything that needs fixing, please let me know in comments. I do know that some photos attached to posts didn’t make it across.

*Followup: Haloscan “upgraded” to a new paid service around the same time as Blogger discontinued their ftp support. Old comments are not transferable.

Leave a Comment

Please donate via your California state tax return to help endangered sea otters

We've donated $50 to the sea otter fund each year since it was added to the California state tax return. Please consider making a donation, however small. Everything helps. From the Monterey Bay Aquarium… > March 9, 2010>> Sea otters are remarkable animals but their struggling population > needs our help.>> Won't you please join [...]

We've donated $50 to the sea otter fund each year since it was added
to the California state tax return. Please consider making a donation,
however small. Everything helps.

From the Monterey Bay Aquarium…

> March 9, 2010
>
> Sea otters are remarkable animals but their struggling population
> needs our help.
>
> Won't you please join me in making a contribution to help sea otters
> when you file your California state income tax form this year?
>
> You need only watch Kit, our three-month old pup and newest addition
> to our sea otter exhibit, to feel inspired to protect the oceans and
> ocean wildlife.
>
> We will never know how Kit became separated from her mother, but we
> do know that females of prime breeding age are dying in record
> numbers. That's why the California Sea Otter Fund is so important–
> it supports critical research needed to help these animals survive.
>
> Your contribution is urgently needed. Last year, the fund fell
> $13,000 short of the $250,000 minimum target. We must meet the
> minimum this year to keep it on the tax form.
>
> Please take action for sea otters when you file your income taxes
> this year.
>
> In 25 years of helping sick and stranded sea otters, we've learned a
> lot but there is much more to do. Please also consider making a tax-
> deductible donation to the Aquarium to support our work on behalf of
> California's sea otters and advance our mission to inspire
> conservation of the oceans.
>
> Thank you,
>
> Julie Packard
> Executive Director
> Monterey Bay Aquarium
>
>
> *****************************************************************
>
> Did You Know?
> Four years ago, a young boy visiting our sea otter exhibit learned
> about the threats facing sea otters and felt compelled to do
> something. He enlisted help from his dad, who happened to be
> California Assembly member Dave Jones. Jones co-authored a bill that
> allowed people to make a voluntary contribution to sea otter
> research when they file their state income taxes, and the California
> Sea Otter Fund was created.
>
> *****************************************************************
>
> The Monterey Bay Aquarium is a non-profit institution. Our mission
> is to inspire conservation of the oceans.
>
> View our Privacy Policy:
> http://www.montereybayaquarium.org/sg/sg_policies.asp
>
> We welcome your thoughts and comments.
> (Send your e-mail to: aquarium@mbayaq.org.)
>
>
>
> Monterey Bay Aquarium | 886 Cannery Row | Monterey, CA 93940 | US

Leave a Comment

Block comments on SFGate.com

The “comments” section under every SFGate.com article is a cesspool (I’m not the first person to say this but I can’t immediately find where it was said first) filled with hate, bigotry, trolling and various evil. Sure, you don’t -have- to click through to the comments but SFGate helpfully shows the three highest-rated comments below [...]

The “comments” section under every SFGate.com article is a cesspool (I’m not the first person to say this but I can’t immediately find where it was said first) filled with hate, bigotry, trolling and various evil. Sure, you don’t -have- to click through to the comments but SFGate helpfully shows the three highest-rated comments below each article–and the worst, and most prolific, commenters have hacking scripts that artificially inflate their comments’ ratings. Here’s another view on SFGate’s comment section: link.

Feeling that “out of sight, out of mind” is a good policy, my first UserScript hides several DIVs associated with comments on SFGate article pages. Here’s the source:

// ==UserScript==// @name SFGate-NoComments// @namespace http://www.userscripts.org// @description Hide comments on SFGate.com articles// @version 0.2// @include http://www.sfgate.com/*// @copyright 2010+, Andrew Rich (http://www.project-insomnia.com)// @license (CC) Attribution Non-Commercial Share Alike; http://creativecommons.org/licenses/by-nc-sa/3.0/// ==/UserScript==

var commentsDiv = document.getElementById('commentspage');commentsDiv.style.display = 'none';var commentslinksSpan = document.getElementById('commentslinks');commentslinksSpan.style.display = 'none';var commentBoxWrapperDiv = document.getElementById('articlePageCommentBoxWrapper');commentBoxWrapperDiv.style.display = 'none';var recCommentsDiv = document.getElementById('sfgate_recommended_comments');recCommentsDiv.style.display = 'none';var commentsListDiv = document.getElementById('commentslist');commentsListDiv.style.display = 'none';var commentsContainerDivAttrs = document.getElementById('Comments_Container_viewall').attributes;commentsContainerDivAttrs.getNamedItem('class').value = '';var commentsContainerDiv = document.getElementById('Comments_Container_viewall');commentsContainerDiv.style.visibility = 'hidden';

I hope this is useful to you and thank you for reading. You can install it easily from the UserScripts.org link above. Tested in Safari 4.0.4 (5531.21.10) with GreaseKit 1.7 on Mac OS X 10.5.8 Build 9L30. Should work in other UserScript-supporting browsers (Firefox, Opera, Chrome “Dev Channel”) without issue.

Leave a Comment

Easy AdSense by Unreal

Project Insomnia is Digg proof thanks to caching by WP Super Cache