Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在寻找加载JS并执行它的优雅方法_Javascript_Jquery_Dynamic_Google Translate - Fatal编程技术网

Javascript 正在寻找加载JS并执行它的优雅方法

Javascript 正在寻找加载JS并执行它的优雅方法,javascript,jquery,dynamic,google-translate,Javascript,Jquery,Dynamic,Google Translate,我是JavaScript新手,我真的很喜欢jQuery,讨厌编写繁琐的代码来完成简单的事情 我目前正在尝试动态加载外部JS并执行它(与Google Translate API通信) 示例代码创建一个脚本标记,设置其src,并将其附加到文档的头以执行它: var newScript = document.createElement('script'); newScript.type = 'text/javascript'; var sourceText = escape(document.getE

我是JavaScript新手,我真的很喜欢jQuery,讨厌编写繁琐的代码来完成简单的事情

我目前正在尝试动态加载外部JS并执行它(与Google Translate API通信)

示例代码创建一个
脚本
标记,设置其
src
,并将其附加到文档的
以执行它:

var newScript = document.createElement('script');
newScript.type = 'text/javascript';
var sourceText = escape(document.getElementById("sourceText").innerHTML);
var source = 'https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&callback=translateText&q=' + sourceText;
newScript.src = source;

// When we add this script to the head, the request is sent off.
document.getElementsByTagName('head')[0].appendChild(newScript);
我想知道jQuery中是否有一个用于此的行程序。

试试这个:

就是为这种用途而设计的,这是一种简单而优化的方式来包含脚本,这将帮助您确定

基本方法:(取800ms)

使用head.js(耗时700毫秒)

head.js(“https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael.js")
.js(”https://github.com/jquery/jquery-ui/raw/master/jquery-1.4.4.js")
.js(”https://github.com/smith/scripty2/raw/master/lib/prototype.js")
.js(”https://github.com/headjs/www/raw/master/content/test/jquery-ui.js")
.js(”https://github.com/kosmas58/compass-jquery-plugin/raw/master/lib/jslint.js");

有(请参见答案),但如果您自己将其放入函数中,您也只需编写一次。:)是的,我的意思是我来自不同的背景(.NET),所以当有一个常用的方法时,我不想在我的代码中传播自定义函数。我对jQuery目前提供的很好,但这是一个很好的补充,以防我需要更复杂的东西。谢谢
$.getScript('https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&callback=translateText&q=' + $('#sourceText').html());
<script src="https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael.js"></script>
<script src="https://github.com/jquery/jquery-ui/raw/master/jquery-1.4.4.js"></script>
<script src="https://github.com/smith/scripty2/raw/master/lib/prototype.js"></script>

<script src="https://github.com/headjs/www/raw/master/content/test/jquery-ui.js"></script>
<script src="https://github.com/kosmas58/compass-jquery-plugin/raw/master/lib/jslint.js"></script>
<script src="../media/js/head.min.js"></script>

<script>
head.js("https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael.js")
     .js("https://github.com/jquery/jquery-ui/raw/master/jquery-1.4.4.js")
     .js("https://github.com/smith/scripty2/raw/master/lib/prototype.js")
     .js("https://github.com/headjs/www/raw/master/content/test/jquery-ui.js")
     .js("https://github.com/kosmas58/compass-jquery-plugin/raw/master/lib/jslint.js");
</script>