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;
        }
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>';
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.

Comments

Your Answer

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.