How to load a javascript function via string
<div id="test_div">display here</div> // < --display here
<script type="text/javascript">
// string
var _script=("<script type=\"text/javascript\">
var disqus_shortname = 'example';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>");
// write/output the string
document.getElementById('test_div').innerHTML= _script;
</script>
asked May 4, 2011 at 12:42
User6996
3,01311 gold badges48 silver badges69 bronze badges
-
dont think it will work and why do you wanna do this???Varun– Varun2011年05月04日 12:45:45 +00:00Commented May 4, 2011 at 12:45
-
1w3schools.com/jsref/jsref_eval.aspMantar– Mantar2011年05月04日 12:45:45 +00:00Commented May 4, 2011 at 12:45
-
What are you trying to do? Do you want the code to run? Or do you want it to display in that div?gen_Eric– gen_Eric2011年05月04日 13:02:50 +00:00Commented May 4, 2011 at 13:02
-
@ yes i want to run this code, Does not even show the messagebox document.getElementById("test_div").innerHTML = ("alert(\".\");");User6996– User69962011年05月04日 13:04:18 +00:00Commented May 4, 2011 at 13:04
-
@Power-Mosfet: If you want to run that script block, just put it in the page, why is it a string?gen_Eric– gen_Eric2011年05月04日 13:11:58 +00:00Commented May 4, 2011 at 13:11
2 Answers 2
JavaScript does not allow new lines inside strings, you need escape each new line with a \.
var _script=("<script type='text/javascript'>\
var disqus_shortname = 'example';\
(function () {\
var s = document.createElement('script'); s.async = true;\
s.type = 'text/javascript';\
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';\
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);\
})();\
</script>");
answered May 4, 2011 at 13:04
gen_Eric
228k42 gold badges304 silver badges343 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Using jQuery
$('#test_div').click(yourFunctionName)
You need to name your function in the script
<script type="text/javascript">
// string
var _script=("<script type=\"text/javascript\">
var disqus_shortname = 'example';
(function yourFunctionName () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>");
I assumed you meant that if you click on the testdiv it should do the function?
Or -- Did you mean you want to load the script dynamically into your webpage?! Ahhhh OK!
Using jQuery Again.
$(body).append('<script type="text/javascript">' + YOUR_JAVA_STRING + '</script>');
answered May 4, 2011 at 12:46
Piotr Kula
9,8918 gold badges62 silver badges88 bronze badges
Comments
lang-js