• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Dynamically add <link> or <script> to page <head>

I am a designer working on some .asp pages.  On some pages I don't have access to the <head> tag.  I want to be able to insert/append either a <link>, <script> or both into the <head> of the page from an include file that is included into the page somewhere in the HTML.

Is this possible?  If so can someone show me how to go about doing just that with an asp tag or function?

Thanks
0
lightrayz
Asked:
lightrayz
1 Solution
 
hieloCommented:

<script>
function importFile(url,type){
  var node=null;
  if(String(type).toLowerCase()=='js')
  {
     node=document.createElement('script');
     node.type='text/javascript';
     node.src=url;
  }
  else if(String(type).toLowerCase()=='css')
  {
      node=document.createElement('link');
      node.type='text/css';
      node.rel='stylesheet';
      node.href=url;
  }
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(node);
}

importFile('styles.css','css');
importFile('yourPlugin.js','js');
</script>

Open in new window

0
 
mattibuttCommented:
hi
some of the script doesnt need to be in the head section of the page if you are using or your team is using asp then the clever way should be like having just one header file it will save time and efforts
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now