Click here to Skip to main content
15,888,610 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: regex help Pin
Kornfeld Eliyahu Peter18-Apr-15 23:07
professionalKornfeld Eliyahu Peter18-Apr-15 23:07 
AnswerRe: regex help Pin
Richard Deeming20-Apr-15 3:23
mveRichard Deeming20-Apr-15 3:23 
GeneralRe: regex help Pin
Wombaticus20-Apr-15 3:28
Wombaticus20-Apr-15 3:28 
QuestionTemplating systems, do we need them ? Pin
Mark C Lester17-Apr-15 22:15
Mark C Lester17-Apr-15 22:15 
Questionhow we can change colors in table cells with time duration Pin
Member 1132187815-Apr-15 20:28
Member 1132187815-Apr-15 20:28 
QuestionUsing server side includes to retrieve Javascript Pin
Mark C Lester13-Apr-15 2:11
Mark C Lester13-Apr-15 2:11 
AnswerRe: Using server side includes to retrieve Javascript Pin
Mark C Lester14-Apr-15 0:58
Mark C Lester14-Apr-15 0:58 
AnswerRe: Using server side includes to retrieve Javascript Pin
Leng Vang2-Jul-15 9:58
Leng Vang2-Jul-15 9:58 
My solution to this exact problem is I created my own function to do it. RequireJS or CommonJS required you to modify your code into their module/package model. I just want to load any js file dynamically and where it is needed. It is cruel but satisfied my need.
The idea is inject the required file into the header of the page and let browser load the file and evaluate it. It works for js and css files.

JavaScript
var ServiceUrl = function() {
  var src = document.currentScript.src;
  var idx = src.indexOf("/scripts");
  if (idx > 0) 
    src = src.substring(0, idx);
  else
    src = "";
  return src;
}();

if (!getServiceUrl) {
  function getServiceUrl() {
    return ServiceUrl;
  }
}

//------------------------
//use for asyn or sync load javascript file on demand.
if (typeof (include) == "undefined") {
  function include(url, type, cb, asIs) {
    asIs = asIs || false;
    cb = cb || null;
    if (!url) {
      console.log("No file specified to be loaded.");
      return;
    }
    if (!asIs)
      url = getServiceUrl() + url;

    var alreadyLoaded = false;
    // Adding the script tag to the head as suggested before
    if (type.indexOf("javascript") > 0) {
      var sc = document.scripts;
      var scExists = false;
      for (var i = 0, len = sc.length; i < len; i++) {
        //<script src="url"></script>
        var src = /src="(.*?)"/.exec(sc[i].outerHTML);
        if (src && src.length > 0) {
          if (src[1] == url) {
            scExists = true;
            break;
          }
        }
      }
      if (!scExists) {
        var el = document.createElement('script');
        el.type = type;
        el.src = url;
      }
      else
        alreadyLoaded = true;
    }
    else {
      var ss = document.styleSheets;
      var cssExists = false;
      for (var i = 0, max = ss.length; i < max; i++) {
        if (ss[i].href == url) {
          cssExists = true;
          break;
        }
      }

      if (!cssExists) {
        var el = document.createElement("link");
        el.href = url;
        el.type = type;
        el.rel = "stylesheet";
      }
      else
        alreadyLoaded = true;
    }

    if (el) {
      // Then bind the event to the callback function.
      // There are several events for cross browser compatibility.
      if (typeof (cb) == "function") {
        el.onload = cb;
      }

      // Fire the loading
      var head = document.getElementsByTagName('head')[0];
      head.appendChild(el);
    }

    if (alreadyLoaded) {
      if (typeof (cb) == "function") {
        cb();
      }
    }
  }
};

QuestionUnable to access popup forum Pin
Sudhir Varma10-Apr-15 19:53
Sudhir Varma10-Apr-15 19:53 
SuggestionRe: Unable to access popup forum Pin
ZurdoDev13-Apr-15 4:20
professionalZurdoDev13-Apr-15 4:20 
QuestionPassing parameter from javascript/AJAX to ASP.NET app Pin
AnkurBhardwaj7-Apr-15 23:29
AnkurBhardwaj7-Apr-15 23:29 
AnswerRe: Passing parameter from javascript/AJAX to ASP.NET app Pin
faizulhaque10-Apr-15 2:41
faizulhaque10-Apr-15 2:41 
Questionhow can we get a selected row value in grid using javascript Pin
Member 113218787-Apr-15 21:23
Member 113218787-Apr-15 21:23 
AnswerRe: how can we get a selected row value in grid using javascript Pin
RajeeshMenoth8-Apr-15 19:35
professionalRajeeshMenoth8-Apr-15 19:35 
Questionjavascript Pin
Member 113218787-Apr-15 19:50
Member 113218787-Apr-15 19:50 
AnswerRe: javascript Pin
Afzaal Ahmad Zeeshan7-Apr-15 22:38
professionalAfzaal Ahmad Zeeshan7-Apr-15 22:38 
AnswerRe: javascript Pin
Anurag Gandhi31-May-15 2:26
professionalAnurag Gandhi31-May-15 2:26 
QuestionHow to multiply layer colors in photoshop with javascript? Pin
Member 1155424825-Mar-15 1:53
Member 1155424825-Mar-15 1:53 
QuestionRefresh only particular Div without reloading whole page Pin
Member 1154215920-Mar-15 8:00
Member 1154215920-Mar-15 8:00 
AnswerRe: Refresh only particular Div without reloading whole page Pin
Richard Deeming20-Mar-15 8:23
mveRichard Deeming20-Mar-15 8:23 
GeneralRe: Refresh only particular Div without reloading whole page Pin
ZurdoDev24-Mar-15 10:04
professionalZurdoDev24-Mar-15 10:04 
QuestionJava Script Slider Pin
Member 1146339917-Mar-15 22:58
Member 1146339917-Mar-15 22:58 
AnswerRe: Java Script Slider Pin
Afzaal Ahmad Zeeshan18-Mar-15 2:14
professionalAfzaal Ahmad Zeeshan18-Mar-15 2:14 
GeneralRe: Java Script Slider Pin
Member 1146339918-Mar-15 2:43
Member 1146339918-Mar-15 2:43 
GeneralRe: Java Script Slider Pin
Afzaal Ahmad Zeeshan18-Mar-15 3:47
professionalAfzaal Ahmad Zeeshan18-Mar-15 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.