Table Of Contents

Previous topic

Autosuggest

Next topic

Page Structure and Navigation

This Page

Javascript Utilities

AJAX - Server Calls

$c(command, args, call_back, on_timeout, no_spinner, freeze_msg)

call a function on the server, where

  • cmd is the command to the handler
  • args dictionary of arguments (form data)
  • call_back - function to be called on complete
  • on_timeout - function to be called on timeout
  • no_spinner - do not show the “Loading...” spinner
  • freeze_msg - freeze the user page while showing with the given message
$c_obj(doclist, method, arg, call_back, no_spinner, freeze_msg)

call an object on the server, where:

  • doclist - the doclist of the calling doc or a string with the name of a Control Doctype
  • method - method to be called
  • arg - string argument
  • call_back - function to be called on completion
  • no_spinner - do not show the “Loading...” spinner
  • freeze_msg - freeze the user page while showing with the given message
$c_obj_csv(doclist, method, arg, call_back, no_spinner, freeze_msg)

call an object on the server and return the output as CSV.

Note: There will be no callback. The output must be a list-in-a-list

$c_js(path, callback)
Load a Javascript library. Path must be relative to the js folder. For example: widgets/calendar.js

Title

title_prefix
Standard prefix to the title
set_title(t)
set Page title, if title_prefix is set, then appends it to the prefix

Events

addEvent(ev, fn) {

Add a listener to the given event. Example:

addEvent('click', function(e, target) { .. });

String

clean_smart_quotes(s)
Returns string with MS Word “Smart” quotes removed
replace_newlines(t)
Replaces newline charcter \n with ‘<br>’
esc_quotes(s)
Returns string with single quote ‘ escaped
strip(s, chars)
Python-like function returns string with leading and lagging characters from chars removed. If chars is null, removes whitespace.
lstrip(s, chars)
Strips chars from left side
rstrip(s, chars)
Strips chars from right side
repl_all(s, s1, s2) {
Replaces all s1 to s2 in s
repl(s, dict)

Python-like string replacement. Example:

s = repl("Hello %(name)s, welcome to %(location)s", {name:'ABC', location:'Mumbai'});
esc_quotes(s)
Returns string with single quote ‘ escaped
strip(s, chars)
Python-like function returns string with leading and lagging characters from chars removed. If chars is null, removes whitespace.
lstrip(s, chars)
Strips chars from left side
rstrip(s, chars)
Strips chars from right side
repl_all(s, s1, s2) {
Replaces all s1 to s2 in s
repl(s, dict)

Python-like string replacement. Example:

s = repl("Hello %(name)s, welcome to %(location)s", {name:'ABC', location:'Mumbai'});

Lists

in_list(list, item)
Returns true if item is in list
inList(list, item)
Returns true if item is in list. Same as in_list
has_common(list1, list2)
Returns true if list1 and list2 has common items

Dictionaries

keys(obj)
Python-like function returns keys of a dictionary
values(obj)
Python-like function returns values of a dictionary
copy_dict(d)
Makes a copy of the dictionary

Datatype Conversion

cint(v, def)
Convert a value to integer, if NaN, then return def
cstr(s)
Convet to string
flt(v, decimals)
Convert to float, with decimal places
fmt_money(v)

Convert number to string with commas for thousands, millions etc and 2 decimals. Example:

fmt_money(2324); // equals '2,324.00'
is_null(v)
Returns true if value is null or empty string. Returns false is value is 0
d2h(d)
Convert decimal to hex
h2d(h)
Convert hex to decimal

DOM Manipulation

$i(id)
Shortcut for document.getElementById(id). Returns the element of the given ID
$a(parent, newtag, className, style)

Add element to the given parent. Example:

div = $a(body, 'div', '', {padding: '2px'});
$a_input(parent, in_type, attributes, style)

Add and INPUT element to the given parent, with given attributes (Fix for IE6 since it does not accept type). Example:

chk = $a_input(body, 'checkbox', null, {border: '0px'});

Style

$y(ele, s)

Set Element style. Example:

$y(div,{height:'4px'});
$dh(d)
Hide element, set display = ‘none’
$ds(d)
set display = ‘block’ (Show element)
$di(d)
set display = ‘inline’ (Show element)
$op(e,w)
Same as set_opacity()
set_style(txt)

Declare css classes in txt. Example:

set_style('div.myclass { width: 400px }');
set_opacity(ele, opacity)

Set the opacity property of the element opacity between 0 and 100

Same As: $op(e,w)

animate(ele, style_key, from, to, callback)
Animate transition of style property
get_darker_shade(col, factor)
Get a darker shade of the given colour, col in HEX, factor between 0 and 1

Tables

make_table(parent, nr, nc, table_width, widths, cell_style)

Make a new table in parent with

  • rows nr
  • columns nc
  • with columns with widths widths
  • cell with default style cell_style

Example:

var t = make_table(div, 5, 2, '400px', ['100px', '300px'], {padding: '2px'})
append_row(t)
Append a new row to the table with same number of columns as the first row
$td(t,r,c)
Returns table cell. Shortcut for t.rows[r].cells[c]
$sum(t, cidx) {
Returns sum of values in a table column
$yt(tab, r, c, s)

Set style on tables with wildcards, Examples:

// hilight row 3
$yt(t, 3, '*', {backgroundColor:'#EEE'})

// border to all cells
$yt(t, '*', '*', {border:'1px solid #000'})

Select Element

empty_select(s)
Empty all OPTIONs of the SELECT (or SelectWidget) element
sel_val(sel)
Returns the selected value of the given SELECT (or SelectWidget) element
add_sel_options(s, list, sel_val, o_style)
Adds options to the SELECT (or SelectWidget) element s, where sel_val is the default selected value

Positioning

objpos(obj)
Returns {x: x-cord, y:y-cord} co-ordinates of the given object (for absolute positioning)
get_screen_dims() {
Returns {w: width, h:height} of the screen

URL

get_url_arg(name)
Return the value of the argument name from the URL

User Image

set_user_img(img, username)
Sets the user image or No Image tag to the given img

Miscelleanous

$s(ele, v, ftype, fopt)

Add the value to the Element ele based on fieldtype and fieldoptions

  • Covnerts Link type to hyperlink
  • Converts Date in user format
  • Converts Check to image with check
validate_email(id)
Returns true for a valid email
Creates a hyperlink to load the record (doctype, name)
Creates a hyperlink to load the record (doctype, name). Same as DocLink