JavaScript addScript function does not work when I load website the first time

I use jquery.flexslider plugin, which works fine.

But for screens < 600px I do not want to load the plugin. I tried several scripts and ended with the following, which I thought works best.

I inserted it after the CSS and the JS tags in the header, but unfortunately it works only on second load, not on the first neither with F5 (tested in IE 11 and in Chrome 45.0.2454.101).

function addScript (src) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = 'www.mywebadre.ss/js/jquery.flexslider-min.js'; 
  document.head.appendChild(script);
} 

if (screen.width > 600) {
  addScript("www.mywebadre.ss/js/jquery.flexslider-min.js");
}

See http://tinyurl.com/qjkeesg

Answer:1

Because of cross-browser differences, you cannot always rely upon document.head already being there. Use document.getElementsById instead to get a reference to the element.

function addScript(src) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
}
if (screen.width > 600) {
    addScript('http://www.mywebadre.ss/js/jquery.flexslider-min.js');
}

Also, I noticed that you were not using http or https on the front of the URL.

After testing this out, it does work in IE and in Chrome so long as I use a valid URL. As a last step, make sure that your URL is valid by pasting it into a browser.

Answer:2

I am creating a Line Chart in D3.js with two lines. The second line needs to start (and be rendered) and at a later date than the first. The data I am using for the graph looks like this: var data = [...

I am creating a Line Chart in D3.js with two lines. The second line needs to start (and be rendered) and at a later date than the first. The data I am using for the graph looks like this: var data = [...

  1. excel leader lines line chart
  2. multiple lines line chart power bi
  3. add vertical lines line chart
  4. 2 lines line chart
  5. hide lines line chart
  6. line chart with multiple lines
  7. line chart with multiple lines excel
  8. line chart not showing lines
  9. line chart (cubic lines) swift
  10. line chart with 2 lines excel
  11. line chart cubic lines android example
  12. line chart too many lines
  13. line chart with multiple lines tableau
  14. line chart with multiple lines in r
  15. line chart with many lines
  16. line chart with two lines
  17. line chart excel two lines
  18. line chart smooth lines
  19. line chart with multiple lines python
  20. line chart with multiple lines chart js

I am using the jQuery Globalize library which uses cldr for internationalization. Before you can use any of the Globalize functions, you have to load the correct cldr-data and pass it to Globalize. ...

I am using the jQuery Globalize library which uses cldr for internationalization. Before you can use any of the Globalize functions, you have to load the correct cldr-data and pass it to Globalize. ...

I am new to CSS. Trying to align these input field validation tooltip this is fiddle: http://jsfiddle.net/fade3r5f/ can anyone help how can I do this.? I can able to move it to left and right but ...

I am new to CSS. Trying to align these input field validation tooltip this is fiddle: http://jsfiddle.net/fade3r5f/ can anyone help how can I do this.? I can able to move it to left and right but ...

How can I pass the site_id parameter to pageTitle from data ? .state('site', { url: "/site/:site_id", templateUrl: "views/site_detail.html", data: {pageTitle: 'Site :' + $scope.$...

How can I pass the site_id parameter to pageTitle from data ? .state('site', { url: "/site/:site_id", templateUrl: "views/site_detail.html", data: {pageTitle: 'Site :' + $scope.$...

  1. angularjs pass state parameter to controller
  2. angularjs pass state params
  3. angularjs pass state parameter
  4. angularjs state pass object
  5. angularjs pass data to state
  6. angularjs pass variable to state
  7. angularjs pass multiple parameters to state