Reference: http://www.planabc.net/2011/05/31/simple_javascript_template_substitute/
Source code:
<! Doctype HTML public "-// W3C // dtd html 4.01 // en"
Http://www.w3.org/TR/html4/strict.dtd" >
< Html >
< Head >
< Script Type = "Text/JavaScript" SRC = "Jquery. Min. js" > </ Script >
< Script Type = "Text/JavaScript" >
Function Substitute (STR, OBJ ){
Alert ( " 456 " );
If ( ! (Object. Prototype. tostring. Call (STR) === ' [Object string] ' )){
Return '' ;
}
// {}, New object (), new class ()
// Object. Prototype. tostring. Call (node = Document. getelementbyid ("XX"): ie678 = '[ object] ', other =' [object htmlelement]'
// 'Isprototypeof 'in node: ie678 === false, other === true
If ( ! (Object. Prototype. tostring. Call (OBJ) === ' [Object object] ' && ' Isprototypeof ' In OBJ )){
Return STR;
}
// Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
Return Str. Replace ( / \ {([^ {}] + )\} / G, Function (Match, key ){
VaR Value = OBJ [Key];
Return (Value ! = Undefined) ? '' + Value: '' ;
});
}
VaR OBJ = {
URL: " Http://www.plannabc.net/ " ,
Title: " Focusing on front-end technologies and user experience " ,
Text: " Feifei's blog "
};
VaR Link = ' <A href = "{URL}" Title = "{Title}"> {text} </a> ' ;
$ ( Function (){
Alert (substitute (link, OBJ ));
});
</ Script >
</ Head >
< Body >
</ Body >
</ Html >
End