Monday, March 19, 2018

[jQuery] Append linux timestamp to all importing external css and javascript files.

Append linux timestamp to all import sentences for external css and javascript files.
you need to have existing import sentence first before append timestamp after file path.
<html>
<head>
<link href="css/flexnav.css" rel="stylesheet" type="text/css">
<link href="css/page.css" rel="stylesheet" type="text/css">
<link href="css/style.css?8888" rel="stylesheet" type="text/css">
<link href="css/index_tabs.css" rel="stylesheet" type="text/css">
<link href="css/jquery.autocomplete.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:700,200' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<script src="js/jquery.js" type="text/javascript"></script>
<script>
// Append linux timestamp to all import sentence for external css and javascript files.
$("link,script[type='text/javascript']").each(function(ind) {
    var thisEle = $(this);
    switch(thisEle.attr("type")){
        case "text/javascript":
            addTimestampToReource(thisEle,'src');
            break;
        case "text/css":
            addTimestampToReource(thisEle,'href');
            break;
    }
});

function addTimestampToReource(ele,attrName){
    if(ele.attr(attrName).indexOf('?') !== -1) return false;
    var timestamp = new Date().getTime();
    ele.attr(attrName,ele.attr(attrName)+'?'+timestamp);
}
</script>
</body>
</html>

No comments :

Post a Comment