Javascript notes

Case-sensitive?
Safari: no
FF3: Yes

Regex in Javascript
To utilize the grouping in javascript regex, you have to use exec() instead of match() or replace().

function getXFromId(id){
        var regex = /x(\d)y(\d)/gi;
        var idMatch = regex.exec(id);
        if(idMatch){
                return idMatch[1];   // the grouping results are storing in an array.
        }
        else{
                return idMatch[1];
        }
}

Default arguments in Javascript[1]
Remember the phrase "default arguments" first, and second remember the traditional way to define default arguments is not working in Javascript. I used JQuery, so the definition can be the following:

function loadBadge(o){
o = $.extend({
        arg1: null,
    arg2: false
}, o || {});
...
o.arg1
o.arg2
...
}

loadBadge({
    arg1:        "hello world",
    arg2:     true
});

loadBadge({
    arg1:        "hello world",
});

"new form.element.observer" is better for textbox observe than keyup.

How to avoid KEY_RETURN auto refresh in Safari?[2]

<script language="JavaScript" type="text/javascript">
  function checkCR(evt) {
    var evt  = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
  }
  document.onkeypress = checkCR;
</script>

How to make getJSON work on cross-domain issue?[3]

1. JSON string has to be free of line breaks;

$.getJSON("http://api/feed/view.php?jsoncallback=?", function(data){
...
});

2. The api server must be generate a method named the same as the value of the callback parameter on the client side;
PHP Code:

<?
    echo $_GET['jsoncallback'];
?> ({ "status":200, "messages":[], "return":{ "Shares":[ { "ID" ....

3. Don't worry about whether to include JS from the API server. It does not matter.

"return" is a reserved word in Javascript, especially in Safari.
It cannot be a object name in JSON, otherwise it will break, totally. One solution is that, to stringfy the JSON object, replace the reserved words, and parse it.

$.getJSON is actually not secured.[4]
The solution is, use $.ajax/$.get, and parse it by yourself.

For some reason, the array inside of a JSON object is stringfied, when I used JQuery and Prototype at the same time.
I have to do the conversion below:

obj = eval('('+str+')');

history.go(-1) doesn't work in Safari?
NOT work in Safari

<a href="#" onclick="history.go(-1);">text</a>

WORK for all the browsers

<a href="javascript:history.go(-1);">text</a>

NO COMPLETE jsonp callback in jQuery[5]
"Currently, there's no easy way to abort a JSONP call once it's been fired, because no reference to the window-scoped callback function (or injected script tag) is returned. Just as an XHR request returns the XHR object, JSONP calls should return a handler that allows callback cancellation. "

Safari 3 can read non-200 callback.

page_revision: 15, last_edited: 1244842697|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License