1

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
0

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Dec 20, 2012 at 20:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.