source: trunk/js/Utilities.js @ 497

Last change on this file since 497 was 497, checked in by anonymous, 10 years ago

Beginning the process of adapting codebase to foundation styles.

File size: 3.1 KB
Line 
1/*
2* The Strangecode Codebase - a general application development framework for PHP
3* For details visit the project site: <http://trac.strangecode.com/codebase/>
4* Copyright © 2014 Strangecode, LLC
5*
6* This program is free software: you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*/
19
20// Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining.
21var Strangecode = Strangecode || {};
22
23/*
24* Emulates a sprintf function.
25---------------------------------------------------------------------
26"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")
27
28outputs
29
30ASP is dead, but ASP.NET is alive! ASP {2}
31---------------------------------------------------------------------
32*
33* @access   public
34* @param    string multiple Strings to pass to the formatted string.
35* @author   http://stackoverflow.com/questions/610406/javascript-printf-string-format/4673436#4673436
36* @version  1.0
37* @since    30 May 2014 18:02:39
38*/
39if (!String.prototype.format) {
40    String.prototype.format = function() {
41        var args = arguments;
42        return this.replace(/{(\d+)}/g, function(match, number) {
43            return typeof args[number] != 'undefined' ? args[number] : match;
44        });
45    };
46}
47
48/*
49* Displays 'user at domain dot com' as 'user@domain.com'.
50---------------------------------------------------------------------
51<span class="sc-email">user at domain dot com</span>
52<a href="mailto:user at domain dot com" class="sc-email">Email me</a>
53<script>
54$('.sc-email').nospam();
55</script>
56---------------------------------------------------------------------
57*
58* @access   public
59* @version  2.0
60* @since    30 Jun 2008 12:32:19
61*/
62jQuery.fn.nospam = function() {
63    return this.each(function(){
64        $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.'));
65        if (this.href) {
66            this.href = this.href.replace(' at ', '@').replace(' dot ', '.');
67        }
68    });
69};
70
71/*
72* Encode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes.
73Then we pull the encoded contents back out. The div never exists on the page.
74---------------------------------------------------------------------
75$('select').append($('<option>', {
76    value: value,
77    text: htmlEncode(text)
78}));
79---------------------------------------------------------------------
80*
81* @access   public
82* @version  1.0
83* @since    30 Jun 2013
84*/
85Strangecode.htmlEncode = function(text) {
86    return $('<div/>').text(text).html();
87};
88jQuery.fn.htmlEncode = Strangecode.htmlEncode;
Note: See TracBrowser for help on using the repository browser.