top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Load CSS & Javascript dynamically in web page?

0 votes
431 views
How to Load CSS & Javascript dynamically in web page?
posted Dec 20, 2017 by Manish Tiwari

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Using SetAttribute we can achieve this.
Example

function loadFile_Js_CSS(filename, filetype) {
  if (filetype == "js") { //if filename is a external JavaScript file
    var fileref = document.createElement('script')
    fileref.setAttribute("type", "text/javascript")
    fileref.setAttribute("src", filename)
  } else if (filetype == "css") { //if filename is an external CSS file
    var fileref = document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
  }
  if (typeof fileref != "undefined")
    document.getElementsByTagName("head")[0].appendChild(fileref)
}
answer Jan 22, 2019 by Anuradha Tabyal
Similar Questions
+1 vote

Am new to JavaScript?Any one please give some sample code for this.

+1 vote

What’s the ideal speed for a site to load a web page. I am working for a website and its showing Very Slow (3.519 Seconds) 84% of sites are faster then this?

0 votes

I want to add a menu to my application screens. The menu will have the menu icons which are horizontal scroll-able one menu at a time when left or right arrow pressed.

...