Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

only add script to head if doesn't exist

I want to add additional scripts and styles to my site when a specific div is loaded. I start out by defining a path to either a script or stylesheet and then create an element. Hereafter I append the element to the head tag in HTML.

But I would like a way to see if the script or stylesheet already has been append before I append it again. It would be stupid to append an already existing script or stylesheet.

Q: How do I use javascript to check wether or not a script already exists in the head tag, and then append the element if not?

EDIT

I have made a function based on the answer from @KernelPanik. It doesn't work yet but hopefully it will. The function is currently in question: my script appending function doesn't work

like image 214
Corfitz. Avatar asked Apr 13 '13 12:04

Corfitz.


People also ask

Can JavaScript be added in head only?

It can go in the head or body tag. Just keep in mind that it will execute whenever is read and not necessarily when the document is finished loading.

Does script tag have to be in head?

The <script> TagScripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Does it matter if script is in head or body?

If your is not placed inside a function, or if your script writes page content, it should be placed in the body section. It is a good idea to place scripts at the bottom of the <body> element. This can improve page load, because script compilation can slow down the display.

How do I create a dynamic script tag?

Dynamic loadingCreate a script element. Set the src , async , and type attributes. Append the script element to the body. Check if the file loaded or not in the load event.


2 Answers

If you can use jquery, this code is good

function appendScript(filepath) {
    if ($('head script[src="' + filepath + '"]').length > 0)
        return;

    var ele = document.createElement('script');
    ele.setAttribute("type", "text/javascript");
    ele.setAttribute("src", filepath);
    $('head').append(ele);
}

function appendStyle(filepath) {
    if ($('head link[href="' + filepath + '"]').length > 0)
        return;

    var ele = document.createElement('link');
    ele.setAttribute("type", "text/css");
    ele.setAttribute("rel", "Stylesheet");
    ele.setAttribute("href", filepath);
    $('head').append(ele);
}

In your code write

appendScript('/Scripts/myScript.js');
appendStyle('/Content/myStyle.css');
like image 141
Bahram Boroumandan Avatar answered Sep 25 '22 11:09

Bahram Boroumandan


var lib = '/public/js/lib.js';

if (!isLoadedScript(lib)) {
  loadScript(lib);
}

// Detect if library loaded
function isLoadedScript(lib) {
  return document.querySelectorAll('[src="' + lib + '"]').length > 0
}

// Load library
function loadScript(lib) {
  var script = document.createElement('script');
  script.setAttribute('src', lib);
  document.getElementsByTagName('head')[0].appendChild(script);
  return script;
}
like image 8
Jack Lee Avatar answered Sep 22 '22 11:09

Jack Lee