我的外部函数从javascript不加载之前的html
My external function from javascript doesnt load before html
我正在使用INTEL XDK和bootstrap主机开发一个移动应用程序。我有Html代码和一些外部js文件与功能运行在它。由于某种原因,JS文件中的函数dosmth()没有在HTML之前加载,所以它没有给我任何结果。(函数应该返回一个字符串数组)。有人能告诉我我的代码有什么问题吗?我错过了什么
包含文件的HTML标题。
<script src="js/file.js"></script>
这是在我的HTML文件中调用函数的代码。
<h4>
<script type="text/javascript">
document.write(dosmth());
</script>
</h4>
js文件中方法的代码。
function getCities()
{
var url = file.api + "name";
console.log(url);
$.getJSON(url).done(function(response){
if (!response.length) {
console.warn("Empty");
}
file.name = response;
$('body').trigger('name-data');
console.log(response);
return (response);
}.fail(function(data, status, error){
console.error("Something went wrong");
});
}
也许这有帮助。
首先,确保HTML标记是可选择的。例如,为这个h4
指定一个id。
<h4 id="cities"></h4>
在调用JS函数之前,你还需要等待文档加载完成:
<script type="text/javascript">
window.onload = function() {
getCities();
);
</script>
我们不返回任何东西,而是更新h4
。
function getCities()
{
var url = file.api + "name";
$.getJSON(url).done(function(response){
if (!response.length) {
console.warn("Empty");
}
// don't know what these two lines are meant to do
file.name = response;
$('body').trigger('name-data');
// update the h4 element
$('#cities').html(response);
// the next line was missing a ")" before ".fail"
}).fail(function(data, status, error){
console.error("Something went wrong");
});
}
相关文章:
- Webpack:如何将原始HTML加载为React JSX
- 画布 HTML 加载时间
- 使用 Ajax 在 html 加载后调用 jquery 插件
- 用javascript(不带外部文件)将html加载到另一个html中
- 错误 404 时,通过 jQuery 将 HTML 加载到 DIV 中
- 如何在将 html 加载到对话框中后更改标签的文本
- 如何将数据从服务器呈现的 html 加载到模型中
- jQuery 从外部 html 加载内容
- Ajax 将 html 加载到页面上并使用页面 js 不起作用
- 如何在没有安全警告的情况下将一个 html 加载到另一个 html 中
- 安卓:根据上一次活动列表中的选择,修改本地静态html加载的JS
- 在AppStore发布时,iOS应用程序上的index.html加载路径不正确
- 使jquery.load在HTML加载之前发生
- 使用jqueryload方法将ajax响应数据作为内部html加载到页面元素中
- HTML加载用户单击按钮时输入的输入字段数
- jQuery Mobile从新html加载第二个页面
- 使用ajax将html加载到Datatable子行中
- 使用JavaScript将HTML加载到DIV
- 使用JS和JQuery替换AJAX中的字符串或html加载
- js函数加载的页面无法使用js函数js-html加载另一个页面