John's Computer Journal: javascript
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, June 4, 2007

Dynamic Image Sizing (Zoom) for Firefox

The following javascript code will setup a HTML document so that in Firefox when the fontsize is increased or decreased, the images will increase or decrease in size as well. The code can be appended to the end of any HTML document you wish or possibly made into a Firefox extension.

Usage

After appending the following code to any HTML document use the Ctrl+Mouse Wheel, the View -> Text Size, Ctrl++, Ctrl+-, or Ctrl+0 menus and or accelerators in Firefox to zoom the page in or out. Internet Explorer and Opera already have the ability to zoom text and images and as far as I know are unaffected by the code.

Limitations

The code does not resize images that are displayed as part of the CSS style.

Javascript Code

<!-- dynamic image sizing - set image size according to font size {{{
     Copyright (C) 2007 John Elkins

     LISCENSE

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the
     Free Software Foundation; either version 2 of the License, or (at your
     option) any later version.

     This program is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
     Public License for more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 

     REQUIREMENTS
            
            Firefox web browser.  IE and Opera already have an image zoom
            functionality.

     USAGE

            Place the following code at the END of the document.  After all
            images have finished loading.

     LIMITATIONS
            
            Images defined by CSS styles are not resized

     Version 1
        
     4 June 2007
-->
<style type="text/css">
<!--
    .em_test_hidden {
        position:absolute;
        left:0px;
        top:-500px;
        width:1px;
        overflow:hidden;
    }
-->
</style>
<script type="text/javascript">
<!--



// Create the test image
var element = document.createElement('img');
element.setAttribute("src", "");
element.setAttribute("style","height:1em;");
element.setAttribute("class","em_test_hidden");
element.setAttribute("id","em_test_element");

document.body.appendChild(element);

element.class="em_test_hidden";

// Use the test image to determine the pixels per em
var px_per_em = document.getElementById("em_test_element").height;

// Set all image heights to change relative to the em
var image = document.getElementsByTagName("img");
for (var i=0; i<image.length; i++) {

    var dynamic_em_height=image[i].height/px_per_em;
    var dynamic_em_width=image[i].width/px_per_em;

    image[i].style.height = dynamic_em_height+"em";
    image[i].style.width  = dynamic_em_width+"em";

}

// change the document so it also resizes accordingly
document.body.style.width = document.width/px_per_em+"em";

-->
</script> <!--}}}-->

Monday, May 28, 2007

Summarize and TOC Blogger Widget

The following widget uses javascript to automatically summarize an article and provide a table of contents when not summarized. During summarization the widget will remove all HTML tags and display by default up to 255 characters or a new line (whichever comes first). When creating a table of contents the widget will search for header tags, add the appropriate anchors, and generate a table of contents with links to the headers in the article.

Requirements

  • Blogger (Beta) Blog
  • Using the layouts templates
  • Javascript
  • Use H1 - H6 to denote section headings

Usage

Summarize
By default the summarize widget will display up to 255 characters or a new line (whichever comes first). To adjust the amount of characters shown in the summary, change the summarize_length variable to the desired length. The summarize_length variable can be accessed through the widget settings screen.
Table of Contents (TOC)
The table of contents is displayed only if there are heading tags within the article. By default headings H1 - H3 are used by blogger to display the article title, date, and comments headings. So, I use H4 - H6 in articles to make things a little easier to read; however H1 - H3 could be use too depending on the style you want.

To change the way the table of contents is displayed edit the .tocH* CSS classes found within the widget settings.

Examples

The summarize and table of contents widget was built for use on John's Computer Journal. You can take a look around and see the widget doing its thing.
Summarize
Check out a non-article page for an example of summarize in action. The index (main) and search pages will summarize all post content.
Table of Contents
Check out the top of this page for an example of the table of contents in action. The table of contents at the top of this page has been dynamically created by the widget's javascript code without having to be hardcoded into the page.

Install

Limitations

  • Since the summarize function removes HTML tags when summarizing, images and formatted text will not appear. To ensure an image shows up for a post when its summarized, use an img tag within the title of the post.
  • The summarize function will mess up google's beta translations.
  • There will be a delay until the page has completely loaded before the summarize or toc functions load and display the results. The delay can be fixed by ensuring the widget is placed after the posts and removing window.onload and just calling the functions directly.
  • The summarize function fails to engage when older posts are viewed through blogger's pagination.

References

Sunday, May 27, 2007

Blogger Widget Generator

If you have a blogger widget you would like to share. Fill in the forms below and click Generate! to build a web form that will allow others to easily add your widget to their own blog.

Widget Information

  • widget.title - (Optional) The title you specify will display in the user's Page Elements tab when they edit their layout. It will also be shown as part of the default widget template. The title can be up to 100 characters long, and may contain well-formed HTML.
  • infoURL - (Optional) You can use this to specify a page on your site that describes your widget or gives other information about it. It will be used as a "more info" link on the setup page people see when adding your widget.
  • logoURL - (Optional) You can add an image URL here, and the image will be displayed next to the "more info" link. It should be thumbnail sized (i.e. less than 100x100 pixels).

  • widget.content - (Required) This is the actual content of your widget, which will be displayed on the user's blog. It can include almost any well-formed code that you want, except for <html> and <body> tags. The widget generator will escape all >'s and <'s and encode scripts.

    Script encoding is a work around that will keep blogger from choking on scripts during form transfer. To disable script encoding, put a space before the script keyword in the script tag; for example: < script> will disable script encoding, <script> will enable script encoding

  • widget.template - (Optional) This should follow the widget tag guidelines. You can also use any of the globally available data tags. If no template is specified, it uses <data:title/> <data:content/> as a default. The template is also escaped and script encoded (see widget.content above).
-->

Reference