topical media & game development

talk show tell print

#javascript-generator-button-digg.js / js



  /* This script and many more are available free online at
  The JavaScript Source!! http://javascript.internet.com
  Created by: Tanya Puntti | http://www.hypergurl.com */
  <!--
  // ==========================================================================
  // JavaScript Tool for making Digg buttons
  // Copyright (C) 2007 Hypergurl
  //
  // Website: http://www.hypergurl.com/digg-generator.html
  // Webmasters can download the script to use on their own sites
  // as long as all comments and links back to http://www.hypergurl.com remain.
  // ==========================================================================
  
  // Set variables for reserved and unreserved characters allowed in text boxes
  
  var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
  var reserved = "!*'();:@&=+,/?%#[]";
  var allowed = unreserved + reserved;
  var hexchars = "0123456789ABCDEFabcdef";
  
  // --------------------------------- Encoding -------------------------------
  
  // This function returns a percent sign followed by two hexadecimal digits.
  // Input is a decimal value not greater than 255.
  function gethex(decimal) 
  {
    return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
  }
  
  // Encode function
  
  function encode() 
  {
    // Clear output field:
    document.converter.urlencoded.value = "";
  
    // Some variables for website address
    var url = document.converter.url.value;
    var urlencoded = "";
  
    // Some variables for title
    var title = document.converter.title.value;
    var titleencoded = "";
  
    // Some variables for description
    var description = document.converter.description.value;
    var descriptionencoded = "";
  
    // Some variables for Topic
    var topic = document.converter.topic.value;
  
      // non-ASCII characters will not be encoded:
      var notascii = "";
  
      // encode website address
          for (var i = 0; i < url.length; i++ ) {
        var ch = url.charAt(i);
        // Check if character is an unreserved character:
        if (unreserved.indexOf(ch) != -1) {
          urlencoded = urlencoded + ch;
        } else {
          // If position in the Unicode table is smaller than 128, then we have
          // an ASCII character:
          var charcode = url.charCodeAt(i);
          if (charcode < 128) {
            urlencoded = urlencoded + gethex(charcode);
          } else {
            urlencoded = urlencoded + ch;
            notascii = notascii + ch + " ";
          }
        
      }
  
          }
  
  // encode title
          for (var j = 0; j < title.length; j++ ) 
          {
        var ch = title.charAt(j);
        // Check if character is an unreserved character:
        if (unreserved.indexOf(ch) != -1)
            {
          titleencoded = titleencoded + ch;
        } 
            else 
            {
          // If position in the Unicode table is smaller than 128, then we have
          // an ASCII character:
          var charcode = title.charCodeAt(j);
          if (charcode < 128)
                  {
            titleencoded = titleencoded + gethex(charcode);
          } 
                  else 
                  {
            titleencoded = titleencoded + ch;
            notascii = notascii + ch + " ";
                  }
       }
           }
  
           // encode description
          for (var x = 0; x < description.length; x++ ) 
          {
        var ch = description.charAt(x);
        // Check if character is an unreserved character:
        if (unreserved.indexOf(ch) != -1)
            {
          descriptionencoded = descriptionencoded + ch;
        } 
            else 
            {
          // If position in the Unicode table is smaller than 128, then we have
          // an ASCII character:
          var charcode = description.charCodeAt(x);
          if (charcode < 128)
                  {
            descriptionencoded = descriptionencoded + gethex(charcode);
          } 
                  else 
                  {
            descriptionencoded = descriptionencoded + ch;
            notascii = notascii + ch + " ";
                  }
       }
           }
  
    // Write result to lower form box
      document.converter.urlencoded.value = "http://digg.com/submit?phase=2&url=" + urlencoded + "&title=" + titleencoded + "&bodytext=" + descriptionencoded + "&topic=" + topic;
  
      // Display warning message if necessary
      if (notascii != "") alert("Warning: Non-ASCII characters in text!\n\nThus, these characters have not been encoded:\n" + notascii);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.