I'm going to implement Defer loading of JavaScript approach because as per current structure JavaScript is being called in <head>
element which is taking long response time. So I just want to add this JavaScript after complete DOM load.
Here is code in cshtml file
Layout.cshtml
<head>
@Scripts.Render("~/bundles/javascript")
</head>
Rendered html view source:
<head>
<script src="/bundles/javascript?v=abcdefghikeriotterpoggkjs-fskdf"></script>
</head>
Here is what I'm planning to do:
I will remove the @Scripts.Render("~/bundles/javascript")
from <head>
section. Then copy the src
value from rendered view source and use given below
<script type="text/javascript">
// Add a script element as a child of the body
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "/bundles/javascript?v=abcdefghikeriotterpoggkjs-fskdf";
document.body.appendChild(element);
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else
window.onload = downloadJSAtOnload;
</script>
Am I doing correct? My doubt is Does this value v=abcdefghikeriotterpoggkjs-fskdf
throughout the application remain same or it can be changed. If it remain same then I think there won't be any issue.
Please suggest.
` tag?
– Nov 26 '16 at 06:27