<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: document code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 14:33:05 GMT</pubDate>
    <description>DZone Snippets: document code</description>
    <item>
      <title>Extract the filename from a URL</title>
      <link>http://snippets.dzone.com/posts/show/5407</link>
      <description>This ECMAScript extracts the filename from a URL. eg. the filename 'index4.svg' would be extracted from the document.URL with the value 'http://rorbuilder.info/r/whiteboardqueue/index4.svg'.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var regexp = /(\w|[-.])+$/&lt;br /&gt;str = document.URL&lt;br /&gt;a = regexp.exec(str)&lt;br /&gt;alert(a[0])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://www.webreference.com/js/column5/methods.html"&gt;Regular Expressions: Methods - Doc JavaScript&lt;/a&gt; [webreference.com]</description>
      <pubDate>Sun, 20 Apr 2008 15:24:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5407</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Reading an XML file using JavaScript</title>
      <link>http://snippets.dzone.com/posts/show/5091</link>
      <description>Reads an xml file using JavaScript from a web page. Files used: loadxml.js (main script), readxml.js (client script), and readxml.html. The code for loadxml.js was copied from the article &lt;a href="http://www.devarticles.com/c/a/JavaScript/JavaScript-and-XML/"&gt;JavaScript and XML&lt;/a&gt; [devarticles.com]&lt;br /&gt;&lt;br /&gt;file: loadxml.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  XML.load = function(url) {&lt;br /&gt;    var xmldoc = XML.newDocument();&lt;br /&gt;    xmldoc.async = false;  &lt;br /&gt;    xmldoc.load(url);&lt;br /&gt;    return xmldoc;  &lt;br /&gt;  };&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;XML.newDocument = function(rootTagName, namespaceURL) {&lt;br /&gt;    if (!rootTagName) rootTagName = "";&lt;br /&gt;    if (!namespaceURL) namespaceURL = "";&lt;br /&gt;&lt;br /&gt;    if (document.implementation &amp;&amp; document.implementation.createDocument) {&lt;br /&gt;        // This is the W3C standard way to do it&lt;br /&gt;        return document.implementation.createDocument(namespaceURL, &lt;br /&gt;                       rootTagName, null);&lt;br /&gt;    }&lt;br /&gt;    else { // This is the IE way to do it&lt;br /&gt;        // Create an empty document as an ActiveX object&lt;br /&gt;        // If there is no root element, this is all we have to do&lt;br /&gt;        var doc = new ActiveXObject("MSXML2.DOMDocument");&lt;br /&gt;&lt;br /&gt;        // If there is a root tag, initialize the document&lt;br /&gt;        if (rootTagName) {&lt;br /&gt;            // Look for a namespace prefix&lt;br /&gt;            var prefix = "";&lt;br /&gt;            var tagname = rootTagName;&lt;br /&gt;            var p = rootTagName.indexOf(':');&lt;br /&gt;            if (p != -1) {&lt;br /&gt;                prefix = rootTagName.substring(0, p);&lt;br /&gt;                tagname = rootTagName.substring(p+1);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            // If we have a namespace, we must have a namespace prefix&lt;br /&gt;            // If we don't have a namespace, we discard any prefix&lt;br /&gt;            if (namespaceURL) {&lt;br /&gt;                if (!prefix) prefix = "a0"; // What Firefox uses&lt;br /&gt;            }&lt;br /&gt;            else prefix = "";&lt;br /&gt;&lt;br /&gt;            // Create the root element (with optional namespace) as a&lt;br /&gt;            // string of text&lt;br /&gt;            var text = "&lt;" + (prefix?(prefix+":"):"") + tagname +&lt;br /&gt;                (namespaceURL&lt;br /&gt;                 ?(" xmlns:" + prefix + '="' + namespaceURL +'"')&lt;br /&gt;                 :"") +&lt;br /&gt;                "/&gt;";&lt;br /&gt;            // And parse that text into the empty document&lt;br /&gt;            doc.loadXML(text);&lt;br /&gt;        }&lt;br /&gt;        return doc;&lt;br /&gt;    }&lt;br /&gt;}; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: readxml.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function readXmlFile() {&lt;br /&gt;  url = 'http://rorbuilder.info/pl/test123';&lt;br /&gt;&lt;br /&gt;  doc = XML.load(url);&lt;br /&gt;  alert(doc.documentElement.firstChild.nextSibling.firstChild.nodeValue);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: readxml.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;  &lt;head&gt;&lt;br /&gt;    &lt;title&gt;Read an XML file&lt;/title&gt;&lt;br /&gt;    &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="js/loadxml.js"/&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="js/readxml.js"/&gt;&lt;br /&gt;  &lt;/head&gt;&lt;br /&gt;&lt;br /&gt;  &lt;body&gt;&lt;br /&gt;    &lt;h1&gt;ReadXML&lt;/h1&gt;&lt;br /&gt;    &lt;p&gt;&lt;br /&gt;       If all goes well you should see an alert box display the message 'testing 123'&lt;br /&gt;   &lt;/p&gt;&lt;br /&gt;    &lt;p&gt;Press the 'go' button to read the xml file '&lt;a href="test123"&gt;test123&lt;/a&gt;' &lt;br /&gt;       from the web server. &lt;br /&gt;       &lt;input type="button" onclick="readXmlFile()" value="go"/&gt;&lt;br /&gt;    &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: To avoid the cross-site security error message 'permission denied', store all the files on the same web server. &lt;br /&gt;&lt;br /&gt;To test the above code try out the &lt;a href="http://rorbuilder.info/pl/readxml.html" title="javascript xml demo"&gt;demo to read the xml file&lt;/a&gt; [rorbuilder.info].</description>
      <pubDate>Sat, 02 Feb 2008 22:53:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5091</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>instapaper bookmarklet</title>
      <link>http://snippets.dzone.com/posts/show/5063</link>
      <description>Assuming you are registered with the new bookmarking service '&lt;a href="http://www.instapaper.com/"&gt;instapaper&lt;/a&gt;', then you would use the following code which would normally be placed as a link within your web browser's toolbar.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,&lt;br /&gt;s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://www.instapaper.com/b',&lt;br /&gt;l=d.location,e=encodeURIComponent,p='?v=3&amp;u='+e(l.href) +'&amp;t='+e(d.title) +'&amp;s='+e(s),&lt;br /&gt;u=f+p;try{if(!/^(.*\.)?instapaper([^.]*)?$/.test(l.host))throw(0);&lt;br /&gt;iptstbt();}catch(z){a =function(){if(!w.open(u,'t','toolbar=0,resizable=0,status=1,&lt;br /&gt;width=250,height=150'))l.href=u;};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);&lt;br /&gt;else a();}void(0)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I'm interested in using similar JavaScript if I ever write my own personal bookmarking service.</description>
      <pubDate>Thu, 31 Jan 2008 15:23:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5063</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
