我只像这样在 html 文档的头部加载过 JS 库
<head>
<script src="somelink.js" />
</head>
我的问题是,我可以从一个函数中加载这个脚本吗?如果可以,那会是什么样子?
function() {
src="somelink.js"
return somemethod.bla;
}
那行得通吗?大概不会。
我有我想要这样做的理由,但现在将它们排除在外。
是否可以从函数中加载库?如果可以,那会是什么样子?
最佳答案
试试这个:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body {
font-size: 12px;
font-family: Arial;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
$( "#success" ).load( "somelink.js", function( response, status, xhr ) {
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
</script>
</body>
</html>
关于javascript - 从函数中加载 js 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945515/