页面加载后加载脚本? [英] Load scripts after page has loaded?

查看:171
本文介绍了页面加载后加载脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以加载某些脚本,例如

Is it possible to load certain scripts like

<script type="text/javascript" src="somescript.js"></script>

当页面的其余部分已加载?

when the rest of the page has loaded?

想象一下,我有一些像这样的大型脚本文件,在加载页面时不需要。例如。我使用的Google Maps API仅在单击按钮时使用(因此不会在页面加载时使用)。

Imagine I have a few larger script files like this that are not needed when the page is loaded. E.g. I'm using the Google Maps API that is only used when a button is clicked (so not on page load).

是否可以加载页面的其余部分首先,在处理我头脑中的所有脚本标签之前?

Is it possible to load the rest of the page first, before processing all those script tags in my head?

推荐答案

在JQuery中你可以在准备好文件时执行此操作

In JQuery you could do this on document ready

$.getScript("somescript.js", function(){
   alert("Script loaded and executed.");
 });

这篇关于页面加载后加载脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆