Skip to content

Instantly share code, notes, and snippets.

@klihelp
Created January 12, 2017 21:22
Show Gist options
  • Select an option

  • Save klihelp/499028199f852f407c5f54cb0f8f38ba to your computer and use it in GitHub Desktop.

Select an option

Save klihelp/499028199f852f407c5f54cb0f8f38ba to your computer and use it in GitHub Desktop.

Revisions

  1. klihelp created this gist Jan 12, 2017.
    32 changes: 32 additions & 0 deletions load-js-from-innerhtml.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    /**
    *
    * @type is typescript
    * @td use better logic for every content load
    */
    var loadJsURL = function(url) {

    var canJsLoad = function(url) {
    if (!url) return false;
    var scripts = document.getElementsByTagName('script');
    for (var i = scripts.length; i--;) {
    // *td
    // better with substring or pos, thinking of // start
    if (scripts[i].src == url) return false;
    }
    return true;
    }

    // Load js url
    var insertJsUrl = function(url) {
    var script = document.createElement('script');
    script.setAttribute('src', url);
    document.body.appendChild(script);
    }

    if ( canJsLoad(url) ) {
    insertJsUrl(url)
    }
    }

    // example
    // loadJsURL('www.website.com/embed.js')