?? ch07_04.htm
字號:
these two companies create the majority of browsers on the web, thereare other high-quality browsers that also support JavaScript andbookmarklets, such as Opera, and these browsers are growing inpopularity. If you start supporting specific browsers, you may findyourself needing to choose which browsers to support and which usersyou are willing to loose. Hopefully, ECMAScript and DOM will quicklyprovide standards across all browsers.</p></div></div><a name="ch07-53404" /><div class="sect2"><h3 class="sect2">7.4.2. Bookmarklets and CGI</h3><p>So what do bookmarklets provide us as <a name="INDEX-1647" /> <a name="INDEX-1,648" /> <a name="INDEX-1,649" />CGI developers?Bookmarklets can do anything that JavaScript can do includingdisplaying dialog boxes, creating new browser windows, and generatingnew HTTP requests. Furthermore, because they execute in the contextof the browser's frontmost window, they can interact withobjects or information in this window without the securityrestrictions that an HTML window from your site would encounter.Thus, bookmarklets provide a very different or even transparentinterface to our CGI scripts.</p><p>Let's look at an example. Say that you want to be able tocreate and store comments for web pages as you surf that you canretrieve when you visit the web pages later. We can do this with asimple bookmarklet and CGI script. First, let's create the CGIscript.</p><p>Our CGI script needs to do two things. It needs to accept a URL and a<a name="INDEX-1650" />commentand record them. It also needs to be able to retrieve a comment whengiven a particular URL. <a href="ch07_04.htm#ch07-76654">Example 7-6</a> provides thecode.</p><a name="ch07-76654" /><div class="example"><h4 class="objtitle">Example 7-6. comments.cgi </h4><a name="INDEX-1651" /><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use CGI;use DB_File;use Fcntl qw( :DEFAULT :flock );my $DBM_FILE = "/usr/local/apache/data/bookmarklets/comments.dbm";my $q = new CGI;my $url = $q->param( "url" );my $comment;if ( defined $q->param( "save" ) ) { $comment = $q->param( "comment" ) || ""; save_comment( $url, $comment );}else { $comment = get_comment( $url );}print $q->header( "text/html" ), $q->start_html( -title => $url, -bgcolor => "white" ), $q->start_form( { action => "/cgi/bookmarklets/comments.cgi" } ), $q->hidden( "url" ), $q->textarea( -name => "comment", -cols => 20, -rows => 8, -value => $comment ), $q->div( { -align => "right" }, $q->submit( -name => "save", -value => "Save Comment" ) ), $q->end_form, $q->end_html;sub get_comment { my( $url ) = @_; my %dbm; local *DB; my $db = tie %dbm, "DB_File", $DBM_FILE, O_RDONLY | O_CREAT or die "Unable to read from $DBM_FILE: $!"; my $fd = $db->fd; open DB, "+<&=$fd" or die "Cannot dup DB_File file descriptor: $!\n"; flock DB, LOCK_SH; my $comment = $dbm{$url}; undef $db; untie %dbm; close DB; return $comment;}sub save_comment { my( $url, $comment ) = @_; my %dbm; local *DB; my $db = tie %dbm, "DB_File", $DBM_FILE, O_RDWR | O_CREAT or die "Unable to write to $DBM_FILE: $!"; my $fd = $db->fd; open DB, "+<&=$fd" or die "Cannot dup DB_File file descriptor: $!\n"; flock DB, LOCK_EX; $dbm{$url} = $comment; undef $db; untie %dbm; close DB;}</pre></blockquote></div><p>We use a disk-based hash called a DBM file in order to store commentsand URLs. The <em class="emphasis">tie</em><a name="INDEX-1652" /> function associates a Perl hash withthe file; then anytime we read from or write to the hash, Perlautomatically performs the corresponding action on the associatedfile. We will <a name="INDEX-1653" />cover how to use DBM files in moredetail in <a href="ch10_01.htm">Chapter 10, "Data Persistence"</a>.</p><p>The<a name="INDEX-1654" />JavaScript that we will use tocall this CGI script is as follows:</p><blockquote><pre class="code">url = document.location.href;open( "http://localhost/cgi/bookmarklets/comments.cgi?url=" + escape( url ), url, "width=300,height=300,toolbar=no,menubar=no" );void( 0 );</pre></blockquote><p>As a <a name="INDEX-1655" />bookmarklet, it looks like this:</p><blockquote><pre class="code">javascript:dOc_uRl=document.location.href;open('http://localhost/cgi/bookmarkletscomments.cgi?url='+escape(dOc_uRl),dOc_uRl,'width=300,height=300,toolbar=no,menubar=no');void( 0 )</pre></blockquote><p>If you save this bookmarklet, visit a web site, and select thebookmarklet from your bookmarks, your browser should display anotherwindow. Enter a comment and save it. Then browse other pages and dothe same if you wish. If you return to the first page and select thebookmarklet again, you should see your original comment for thatpage, as in <a href="ch07_04.htm#ch07-79956">Figure 7-5</a>. Note that the commentswindow will not update itself each time you travel to another page.You will need to select the bookmarklet each time you want to read orsave comment for a page you are on.</p><a name="ch07-79956" /><div class="figure"><img width="253" src="figs/cgi2.0705.gif" height="132" alt="Figure 7-5" /></div><h4 class="objtitle">Figure 7-5. Updating a comment to comment.cgi via a bookmarklet</h4><p>If you were to distribute this bookmarklet to friends, the commentswould be shared and you could see what each other has to say aboutvarious web sites. The CGI script could also be placed in a securedirectory and be extended to maintain separate databases for eachuser; you may want users to only be able to read other users'comments.</p><p>We would not have been able to build an application like this with astandard HTML page due to JavaScript's<a name="INDEX-1656" />securityrestrictions. One HTML page cannot access objects in another HTMLpage if the two pages are from different domains (i.e., different webservers), so our comment form cannot determine the URL of any otherbrowser windows. However, bookmarklets circumvent this restriction.Browsers allow this because the user must actively choose to run abookmarklet in order for it to execute.</p><p>There are numerous other ways that you can put bookmarklets to use.You can see many examples of <a name="INDEX-1657" /><a name="INDEX-1658" />bookmarklets that use existingInternet resources at <a href="http://www.bookmarklets.com">http://www.bookmarklets.com</a>. Many of theseare novelties, but bookmarklets can do more. Bookmarklets are mostpowerful when you have goods or services that can take advantage ofaccessing information on other sites as people surf. For example,companies such as the Better Business Bureau could offer bookmarkletsthat users can select when they are on another site to see how thatsite has been rated. Companies that sell add-on products or serviceslike warranties can provide users with a bookmarklet that users canselect when they are going to make <a name="INDEX-1659" />a purchase online. <a name="INDEX-1,660" /> <a name="INDEX-1,661" /> <a name="INDEX-1,662" />Other possibilitiesare <a name="INDEX-1663" /><a name="INDEX-1664" />up to youto <a name="INDEX-1665" />create.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch07_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch08_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">7.3. Data Exchange</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">8. Security</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -