admin管理员组文章数量:1341409
This question looks a little dumb... but I am wondering if is it possible to insert in-line javascript code into another javascript code. Lets put this clear:
I'm trying to put a google plus button, appending it through jquery as follows:
jQuery('.gplus').append('<g:plusone annotation="inline"></g:plusone>');
This button needs external js in order to work properly. What i'm trying to do is insert the external js doing this:
var gplusjs = '
<script type="text/javascript">
(function() {var po = document.createElement("script");
po.type = "text/javascript"; po.async = true;
po.src = ".js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(po, s);
})();
</script>';
jQuery('#js').append(gplusjs);
The result of this is the code printed as HTML, but not interpreted as a script. This is possible? I am a dumbass? Thanks!
This question looks a little dumb... but I am wondering if is it possible to insert in-line javascript code into another javascript code. Lets put this clear:
I'm trying to put a google plus button, appending it through jquery as follows:
jQuery('.gplus').append('<g:plusone annotation="inline"></g:plusone>');
This button needs external js in order to work properly. What i'm trying to do is insert the external js doing this:
var gplusjs = '
<script type="text/javascript">
(function() {var po = document.createElement("script");
po.type = "text/javascript"; po.async = true;
po.src = "https://apis.google./js/plusone.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(po, s);
})();
</script>';
jQuery('#js').append(gplusjs);
The result of this is the code printed as HTML, but not interpreted as a script. This is possible? I am a dumbass? Thanks!
Share Improve this question asked Jan 10, 2012 at 23:44 ManuManu 5731 gold badge5 silver badges18 bronze badges 02 Answers
Reset to default 11That's because jQuery append and scripts don't work. Do it the old fashioned way
var gplusjs = '(function() {var po = document.createElement("script");po.type = "text/javascript"; po.async = true;po.src = "https://apis.google./js/plusone.js";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(po, s);})();';
var script = document.createElement("script");
script.textContent = gplusjs;
document.head.appendChild(script);
Also note that multi line string literals don't exist in JS.
You should just execute that code normally:
var po = document.createElement("script");
po.type = "text/javascript"; po.async = true;
po.src = "https://apis.google./js/plusone.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(po, s);
Or, even easier:
$.getScript("https://apis.google./js/plusone.js");
本文标签: Is it possible to insert inline javascript code into another javascriptjquery codeStack Overflow
版权声明:本文标题:Is it possible to insert in-line javascript code into another javascriptjquery code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743671083a2519545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论