Javascript位于底部,函数调用在体内?
问题描述:
一个简单的问题,我遵循的惯例是将所有Javascript文件保留在文档底部关闭主体之前.但是我想在主体中调用一个函数,该函数会出现在JS include之前,因此会失败.
Just a quick question, I'm following the practice of keeping all Javascript files at the bottom of the document before the closing body. However I want to call a function in the body, which would appear before the JS include, and thus fails.
是否有必要使此功能正常运行而又不将文件移入头部?
Is there anyway to get this function to work without moving the files into the head?
干杯!
答
是的,您可以将函数包装为
yes you can wrap your function with
window.onload = function() {
//call to your function here
};
,但是如果您可以修改页面底部,则此解决方案可能会更好:
停止支付jQuery税
but probably this solution is better if you can modify the bottom part of your page:
Stop paying your jQuery tax