Solved

Dynamically load js or css file into DOM - question

Posted on 2008-10-20
6
525 Views
Last Modified: 2009-04-08
Hello,

I am using the code below to load js and css dynamically. I just want to know when I am calling it in a page will it load multiple times or will it load only instance if it already exists in DOM.

Please advise

function loadjscssfile(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)

}

Open in new window

0
Comment
Question by:krisred
6 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
Comment Utility
not sure I understand the question....


when you call that function to include a .js file.. assuming you fulfill the arguments properly, then it will include it in the DOM. It wont check to see if it's already been included or not.
0
 
LVL 8

Assisted Solution

by:MatthiasVance
MatthiasVance earned 50 total points
Comment Utility
Every time you run that function with the same arguments, a new child will be added to the heading.
You could however, write a function that checks for existence of the element you're going to create in the DOM.

Kind regards,

Matthias Vance
0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
Comment Utility
>>will it load only instance if it already exists in DOM
It will create a new node every time you call the function. If you want to avoid try:
function loadjscssfile(filename, filetype){

 var fid=filename.replace( /[^a-z]/ig,'');

 if( null !==document.getElementById(fid) )

 {

   //file already "imported"

  return false;

 }

 if (filetype=="js"){ //if filename is a external JavaScript file

  var fileref=document.createElement('script')

  fileref.setAttribute("type","text/javascript")

  fileref.setAttribute("src", filename)

  fileref.setAttribute("id", fid)

 }

 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)

  fileref.setAttribute("id", fid)

 }

 if (typeof fileref!="undefined")

  document.getElementsByTagName("head")[0].appendChild(fileref);

return fileref;

}

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:krisred
Comment Utility

What I am doing is calling javascript and css to load when a link in clicked. it loads the data in a pane using ajax. but I don't want to do is keep reloading the same javascript and css every time the link is clicked.

so do you advise to remove the instance and reload using the following function
function removejscssfile(filename, filetype){

 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from

 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for

 var allsuspects=document.getElementsByTagName(targetelement)

 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove

  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)

   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()

 }

}

Open in new window

0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 50 total points
Comment Utility
There is no need to remove and reload. I would recommend using hielo's function, or a variant of it, such as your loop search, to just skip over loading a CSS or Javascript file if it has already been loaded.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>but I don't want to do is keep reloading the same javascript and css every time the link is clicked.
What I posted avoids exactly that problem.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Form submit issue 11 52
angularls and plnkr 14 17
Uncaught TypeError: can't read property 1 13
Printing a Google Form 2 0
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now