 <p>Please check out my edit. If you <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify" rel="nofollow"><strong>JSON.stringify</strong></a> your object, then your code will work as you expect.</p> <pre><code>var gmarkers = {key: 'value'}; var ss = document.createElement('script'); var scr = "function caller(){ alert("+JSON.stringify(gmarkers)+") ;}"; //alert(scr); var tt1 = document.createTextNode(scr); ss1.appendChild(tt1); var hh1 = document.getElementsByTagName('head')[0]; hh1.appendChild(ss1); caller() var tt = document.createTextNode(scr); ss.appendChild(tt); var hh = document.getElementsByTagName('head')[0]; hh.appendChild(ss); caller(); </code></pre>