admin管理员组文章数量:1200772
I have a number of pages for my website all use jQuery and JSON and the same CSS, except for a few pages. The first page is user login. As the user will take time to type in his username and password, I want to download all the required JavaScript and CSS files for the entire user session during login. How can this be done? The header is the same for all pages. How do I optimize it?
I have a number of pages for my website all use jQuery and JSON and the same CSS, except for a few pages. The first page is user login. As the user will take time to type in his username and password, I want to download all the required JavaScript and CSS files for the entire user session during login. How can this be done? The header is the same for all pages. How do I optimize it?
Share Improve this question edited Jul 31, 2012 at 18:14 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Jul 31, 2012 at 8:23 rohitnaidu19rohitnaidu19 6935 silver badges23 bronze badges 4 |4 Answers
Reset to default 15My idea would be load in js and css files dynamically after document.load
. This would not affect the load time of the login page, whilst also caching your js and css files once the user has logged in.
You could also easily change this to document.ready
if it loads faster for you.
What about something like this?
$(document).load(function() {
function preloadFile(filename, filetype){
//For javascript files
if (filetype=="js"){
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
}
//For CSS files
else if (filetype=="css") {
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
document.getElementsByTagName("head")[0].appendChild(fileref)
}
//Examples of how to use below
preloadFile("myscript.js", "js");
preloadFile("javascript.php", "js");
preloadFile("mystyle.css", "css");
});
References
http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
- Add all your css/javascript on your login page.
- Put all your css at the top in HEAD section.
- Put all your Javascripts at the bottom.
Also make your Javascripts and CSS as external files as they are cached by browser(rather than writing the inline javascript and css in the page).
Minify your Javascript and CSS
UPDATE 2
Post loading of JS and CSS could also be done using YUI GET UTILITY
For more details look at this Yahoo : Best Practises for Optimizing your WebSite
i want to download all the required javascript and css files for entire user session during login
<title>Login</title>
<script src="foo.js"></script> <!-- This line might be better placed just before </body> -->
<link rel="stylesheet" type="text/css" href="foo.css">
I'd recommend an asynchronous loader e.g. requireJS.
Once you've created and compressed your javascript and css, assign the loading of it to an event, for instance when the user focuses on the username field.
Facebook uses a similar trick with their typeahead queries: loading data relevant to the user when the search box gains focus.
本文标签: phpOptimize JavaScript CSS downloadStack Overflow
版权声明:本文标题:php - Optimize JavaScript CSS download - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738621778a2103232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for entire user session
– Adi Commented Jul 31, 2012 at 8:37