Is there a better way to inject a dynamic text into the html string other than the code specified below?
var code = 'siteCode1';
html code as a string:
existing: '<p>Log in with your'+ eval("siteVarObj('+code+').siteName") +'account</p>',
function siteVarObj(siteCode){
if(siteCode === 'siteCode1'){
this.siteName = 'google.com';
this.siteContactUsURL = '';
}else if(siteCode === 'siteCode2'){
this.siteName = 'stackoverflow.com';
this.siteContactUsURL = '';
}
return this;
}
asked Dec 20, 2012 at 19:57
neelmeg
2,5056 gold badges34 silver badges47 bronze badges
2 Answers 2
This should work just fine:
var code = 'siteCode1';
var myHTMLString = '<p>Log in with your '+ siteVarObj(code).siteName + ' account</p>';
answered Dec 20, 2012 at 19:59
BLSully
5,9191 gold badge29 silver badges46 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I would generally suggest to avoid execution of any input from within eval because eval opens up your code for injection attacks when used inproperly.
Comments
default