Tag Archive for 'javascript'

Google Maps API experiment, revisited

Apparently, I goofed.

When I visited my Google Maps API project this morning, the map was missing! Thanks to Migs Paraz for pointing that out.

In the previous version, I added a few div items that shouldn’t be there (in the spirit of separating content, presentation and behavior in the code). I cleaned the code a bit: completely separating the Javascript code to another file so the HTML will not be cluttered, and added an if (GBrowserIsCompatible()) check.

The XML is still as it is. Will have to consult with the content developers on what other info to include. And I’m still not sure about the GPS locations because I just gathered the samples from Global Gazetteer Version 2.1. Will have to check with the GIS people on Monday.

For now, here it is. It’s really not that much: there’s a list of interesting points on the left that you can click to display info on the map. I’ll be adding the relevant information soon, such as the websites, statistics and related links. The points, by the way, are some areas where our project is currently working in.

Roll your own del.icio.us link roll

There is, of course, a ready-made Javascript code to include the latest del.icio.us postings to your blog or web page, but if you want to ‘beautify’ it, take a look at what I’ve done.

I used del.icio.us’ JSON object, named Delicious.posts that contains the latest bookmarks. I snatched the code from the del.icio.us JSON help page and added a few enhancements, like the extended note and the associated tags.

This is the code:

<h2 class="sidebar-title">latest del.icio.us postings</h2>
<div id="delicious-posts"></div>
<script type="text/javascript" src="http://del.icio.us/feeds/json/iandexter \\
   ?count=5"></script>
<script type="text/javascript">
/**
 * Delicious linkroll - Modified code from del.icio.us link roller.
 * Ian Dexter R. Marquez, 2006
 */
 document.write('<style type="text/css">#delicious-posts ul{ \\
   list-style-type:none}#delicious-posts span{ \
   font-size:smaller;margin:0;}</style>');
 function showImage(img) { return (function(){ img.style.display='inline'; }) }
 var ul = document.createElement('ul');
 for(var i=0; i<Delicious.posts.length; i++) {
  var post = Delicious.posts[i];
  var li = document.createElement('li');
  var url = document.createElement('a');
  var note = document.createElement('span');
  var img = document.createElement('img');
  img.style.display = 'none';
  img.style.padding = '0 2px 0 0';
  img.height = img.width = 16;
  img.src = post.u.split('/').splice(0,3).join('/')+'/favicon.ico';
  img.onload = showImage(img);
  url.setAttribute('href', post.u);
  url.appendChild(document.createTextNode(post.d));
  if(post.t) {
   note.appendChild(document.createTextNode(' / '));
   for(var j=0; j<post.t.length; j++) {
    var tag = document.createElement('a');
    var tagu = 'http://del.icio.us/iandexter/' + post.t[j];
    tag.setAttribute('href', tagu);
    tag.appendChild(document.createTextNode(post.t[j]));
    note.appendChild(tag);
    note.appendChild(document.createTextNode(' '));
   }
  }
  if(post.n) {
   var post_note = post.n ? post.n : '';
   url.setAttribute('title', post_note);
   note.appendChild(document.createElement('br'));
   note.appendChild(document.createTextNode(post_note));
  }
  li.appendChild(img);
  li.appendChild(url);
  li.appendChild(note);
  ul.appendChild(li);
 }
 document.getElementById('delicious-posts').appendChild(ul);
</script>
<noscript><h2 class=”sidebar-title”><a href=”http://del.icio.us/iandexter”> \\
   latest del.icio.us postings</a></h2></noscript>

First, set up the HTML container for the heading and the postings. The Delicious.posts object is fetched using:

<script type=”text/javascript” src=”http://del.icio.us/feeds/json/iandexter \\
   ?count=5"></script>

The script iterates through the object, parsing the URL (Delicious.posts[i].u), description (d), and extended note, if any (n). The inner j loop iterates through the tags (t).

The script also fetches the page icon of each link.

To roll your own, just replace the username in:

<script type=”text/javascript” src=”http://del.icio.us/feeds/json/username \\
   ?count=5"></script>

and

    var tagu = 'http://del.icio.us/iandexter/' + post.t[j];

Paste in the code somewhere in body of your web page, and you got yourself a yummy link roller.

“Bookmark this site” script

I know this has been done elsewhere — and better. Just want to put down for posterity.

<script type="text/javascript" language="JavaScript1.2">
var url = "url_goes_here";
var desc = "text_description_here";
function BookMark() {
  var ver = navigator.appName;
  var num = parseInt(navigator.appVersion);
  if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
    window.external.AddFavorite(url,desc);
  } else {
    alert("To bookmark, press Ctrl-D");
  }
}
</script>

Still looking for Gecko alternative…

Update: By gulay! I got it!

<a href="url_goes_here" onClick="javascript:BookMark();" rel="sidebar" \\
   title="Site_title_here">Bookmark this site</a>

Geez, it was that simple.

Update: Here’s a more compact version, without the need for the Javascipt above.

<a href="url_goes_here" onclick="javascript:window.external.AddFavorite \\
   ('url_goes_here','page title');" rel="sidebar" \\
title="page title">Bookmark this site</a>