2012-12-20 20 views
1

有沒有更好的方法將動態文本注入到除下面指定的代碼之外的html字符串中?在一個字符串內執行javascript函數

var code ='siteCode1';

HTML代碼作爲字符串:

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; 
     } 
+2

'eval' ==='evil' –

回答

5

這應該只是罰款:

var code = 'siteCode1'; 
var myHTMLString = '<p>Log in with your '+ siteVarObj(code).siteName + ' account</p>'; 
0

我一般會建議,以避免任何輸入的執行從eval裏,因爲 EVAL打開了你的代碼用於正確使用注射攻擊。

相關問題