Load external javascript when button is clicked












0















As a start, Javascript is way out of my comfort zone. On my website I want to show a map, however the load time is pretty high and contains a lot of requests. So in order to for come a long page load, why not let the user click a button and than load the javascript. One restriction, the map should be displayed in a specific row and column.



What I have so far, the area and a button:



<div class="row">
<div class="col-4">
<input type="button" class="button-class" onclick="myFunc(this)">
<script type="text/javascript">
var width='100%'; // the width of the embedded map in pixels or percentage
var height='300'; // the height of the embedded map in pixels or percentage
var border='1'; // the width of the border around the map (zero means no border)
var shownames='true'; // to display ship names on the map (true or false)
var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
var zoom='11'; // the zoom level of the map (values between 2 and 17)
var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
// Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
function myFunc(e) {
if ("https:" == document.location.protocol) {
/* secure */
var src = (typeof localembed != 'undefined') ? 'https://marinetraffic.local/' : 'https://www.marinetraffic.com/';
} else {
/* unsecure */
var src = (typeof localembed != 'undefined') ? 'http://marinetraffic.local/' : 'http://www.marinetraffic.com/';
}

if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
window.latitude = 0;
window.longitude = 0;
}

if (typeof window.mtembedcode != "undefined") {
var overridenLatLon = '';
if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
if(window.zoom === undefined){
window.zoom = 3;
}
overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
}
document.write(
'<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
);
} else {
document.write(
'<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
);
}
}
// src="{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js";
//window.onload = init();
</script>
</div>
</div>


This will open the map in the same window without leaving the website as is.



For the expert probably pretty simple to solve.... Someone willing to help me out?










share|improve this question



























    0















    As a start, Javascript is way out of my comfort zone. On my website I want to show a map, however the load time is pretty high and contains a lot of requests. So in order to for come a long page load, why not let the user click a button and than load the javascript. One restriction, the map should be displayed in a specific row and column.



    What I have so far, the area and a button:



    <div class="row">
    <div class="col-4">
    <input type="button" class="button-class" onclick="myFunc(this)">
    <script type="text/javascript">
    var width='100%'; // the width of the embedded map in pixels or percentage
    var height='300'; // the height of the embedded map in pixels or percentage
    var border='1'; // the width of the border around the map (zero means no border)
    var shownames='true'; // to display ship names on the map (true or false)
    var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
    var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
    var zoom='11'; // the zoom level of the map (values between 2 and 17)
    var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
    var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
    var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
    // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
    function myFunc(e) {
    if ("https:" == document.location.protocol) {
    /* secure */
    var src = (typeof localembed != 'undefined') ? 'https://marinetraffic.local/' : 'https://www.marinetraffic.com/';
    } else {
    /* unsecure */
    var src = (typeof localembed != 'undefined') ? 'http://marinetraffic.local/' : 'http://www.marinetraffic.com/';
    }

    if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
    window.latitude = 0;
    window.longitude = 0;
    }

    if (typeof window.mtembedcode != "undefined") {
    var overridenLatLon = '';
    if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
    if(window.zoom === undefined){
    window.zoom = 3;
    }
    overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
    }
    document.write(
    '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
    );
    } else {
    document.write(
    '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
    );
    }
    }
    // src="{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js";
    //window.onload = init();
    </script>
    </div>
    </div>


    This will open the map in the same window without leaving the website as is.



    For the expert probably pretty simple to solve.... Someone willing to help me out?










    share|improve this question

























      0












      0








      0








      As a start, Javascript is way out of my comfort zone. On my website I want to show a map, however the load time is pretty high and contains a lot of requests. So in order to for come a long page load, why not let the user click a button and than load the javascript. One restriction, the map should be displayed in a specific row and column.



      What I have so far, the area and a button:



      <div class="row">
      <div class="col-4">
      <input type="button" class="button-class" onclick="myFunc(this)">
      <script type="text/javascript">
      var width='100%'; // the width of the embedded map in pixels or percentage
      var height='300'; // the height of the embedded map in pixels or percentage
      var border='1'; // the width of the border around the map (zero means no border)
      var shownames='true'; // to display ship names on the map (true or false)
      var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
      var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
      var zoom='11'; // the zoom level of the map (values between 2 and 17)
      var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
      var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
      var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
      // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
      function myFunc(e) {
      if ("https:" == document.location.protocol) {
      /* secure */
      var src = (typeof localembed != 'undefined') ? 'https://marinetraffic.local/' : 'https://www.marinetraffic.com/';
      } else {
      /* unsecure */
      var src = (typeof localembed != 'undefined') ? 'http://marinetraffic.local/' : 'http://www.marinetraffic.com/';
      }

      if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
      window.latitude = 0;
      window.longitude = 0;
      }

      if (typeof window.mtembedcode != "undefined") {
      var overridenLatLon = '';
      if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
      if(window.zoom === undefined){
      window.zoom = 3;
      }
      overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
      }
      document.write(
      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
      );
      } else {
      document.write(
      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
      );
      }
      }
      // src="{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js";
      //window.onload = init();
      </script>
      </div>
      </div>


      This will open the map in the same window without leaving the website as is.



      For the expert probably pretty simple to solve.... Someone willing to help me out?










      share|improve this question














      As a start, Javascript is way out of my comfort zone. On my website I want to show a map, however the load time is pretty high and contains a lot of requests. So in order to for come a long page load, why not let the user click a button and than load the javascript. One restriction, the map should be displayed in a specific row and column.



      What I have so far, the area and a button:



      <div class="row">
      <div class="col-4">
      <input type="button" class="button-class" onclick="myFunc(this)">
      <script type="text/javascript">
      var width='100%'; // the width of the embedded map in pixels or percentage
      var height='300'; // the height of the embedded map in pixels or percentage
      var border='1'; // the width of the border around the map (zero means no border)
      var shownames='true'; // to display ship names on the map (true or false)
      var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
      var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
      var zoom='11'; // the zoom level of the map (values between 2 and 17)
      var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
      var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
      var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
      // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
      function myFunc(e) {
      if ("https:" == document.location.protocol) {
      /* secure */
      var src = (typeof localembed != 'undefined') ? 'https://marinetraffic.local/' : 'https://www.marinetraffic.com/';
      } else {
      /* unsecure */
      var src = (typeof localembed != 'undefined') ? 'http://marinetraffic.local/' : 'http://www.marinetraffic.com/';
      }

      if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
      window.latitude = 0;
      window.longitude = 0;
      }

      if (typeof window.mtembedcode != "undefined") {
      var overridenLatLon = '';
      if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
      if(window.zoom === undefined){
      window.zoom = 3;
      }
      overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
      }
      document.write(
      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
      );
      } else {
      document.write(
      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
      );
      }
      }
      // src="{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js";
      //window.onload = init();
      </script>
      </div>
      </div>


      This will open the map in the same window without leaving the website as is.



      For the expert probably pretty simple to solve.... Someone willing to help me out?







      javascript html






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 17:33









      user2037412user2037412

      528




      528
























          3 Answers
          3






          active

          oldest

          votes


















          1














          If you are going to be changing your scripts around, I'd suggest organizing your scripts into files outside of your html and investigating using prototypes. I load almost 100 different html, js, and css fragments so have some utilities that do most of the legwork for me. That said, if you are just wanting to do a simple script add, you can do it like this:



          Inside your button click event handler do the following.



          with jQuery: (recommended unless project is quite simple)



          var script = document.createElement('script');
          script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
          script.type = 'text/javascript';
          $('head')[0].appendChild(script);


          without jQuery:



          var script = document.createElement('script');
          script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
          script.type = 'text/javascript';
          document.getElementsByTagName('head')[0].appendChild(script);


          The script will be added and executed. If this your own script file, an alternative would be to just wrap the code in a function and only call that function during the button click handler.






          share|improve this answer































            1














            this function loads external script, just add this to your condition



            function loadScript(url, callback) {
            var script = document.createElement("script")
            script.type = "text/javascript";
            if (script.readyState) { //IE
            script.onreadystatechange = function () {
            if (script.readyState == "loaded" ||
            script.readyState == "complete") {
            script.onreadystatechange = null;
            callback();
            }
            };
            } else {
            script.onload = function () {
            callback();
            };
            }
            script.src = url;
            document.getElementsByTagName("head")[0].appendChild(script);
            }


            here is use example



            loadScript("[SCRIPT_URL]", function () { 
            //do something after script loads
            });





            share|improve this answer


























            • Please always link back to the source, or mention the attribution when using code from others.

              – rlemon
              Nov 22 '18 at 18:13











            • @rlemon sorry, i will add on next time, thanks for add

              – Demian Bibiano
              Nov 22 '18 at 18:21



















            0














            Put the script in a separate .js file. Removed the function myFunc from the script. Next changed the code between the script-tags:



            <input type="button" class="button-class" onclick="myFunc(this)">
            <script type="text/javascript">
            function myFunc(e) {
            var script = document.createElement('script');
            script.src = 'http://localhost/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
            script.type = 'text/javascript';
            $('head')[0].appendChild(script);
            }
            </script>


            Marinetraffic.js



            var width='100%';   // the width of the embedded map in pixels or percentage 
            var height='300'; // the height of the embedded map in pixels or percentage
            var border='1'; // the width of the border around the map (zero means no border)
            var shownames='true'; // to display ship names on the map (true or false)
            var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
            var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
            var zoom='11'; // the zoom level of the map (values between 2 and 17)
            var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
            var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
            var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
            // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
            (function() {

            if ("https:" == document.location.protocol) {
            /* secure */
            var src = 'https://www.marinetraffic.com/';
            } else {
            /* unsecure */
            var src = 'http://www.marinetraffic.com/';
            }

            if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
            window.latitude = 0;
            window.longitude = 0;
            }

            if (typeof window.mtembedcode != "undefined") {
            var overridenLatLon = '';
            if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
            if(window.zoom === undefined){
            window.zoom = 3;
            }
            overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
            }
            document.write(
            '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
            );
            } else {
            document.write(
            '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
            );
            }
            })()

            //
            // EOF
            //


            This leaves me a warning in the FireFox developer log:




            A call to document.write() from an asynchronously-loaded external
            script was ignored.




            Any thoughts?






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435910%2fload-external-javascript-when-button-is-clicked%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              If you are going to be changing your scripts around, I'd suggest organizing your scripts into files outside of your html and investigating using prototypes. I load almost 100 different html, js, and css fragments so have some utilities that do most of the legwork for me. That said, if you are just wanting to do a simple script add, you can do it like this:



              Inside your button click event handler do the following.



              with jQuery: (recommended unless project is quite simple)



              var script = document.createElement('script');
              script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
              script.type = 'text/javascript';
              $('head')[0].appendChild(script);


              without jQuery:



              var script = document.createElement('script');
              script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
              script.type = 'text/javascript';
              document.getElementsByTagName('head')[0].appendChild(script);


              The script will be added and executed. If this your own script file, an alternative would be to just wrap the code in a function and only call that function during the button click handler.






              share|improve this answer




























                1














                If you are going to be changing your scripts around, I'd suggest organizing your scripts into files outside of your html and investigating using prototypes. I load almost 100 different html, js, and css fragments so have some utilities that do most of the legwork for me. That said, if you are just wanting to do a simple script add, you can do it like this:



                Inside your button click event handler do the following.



                with jQuery: (recommended unless project is quite simple)



                var script = document.createElement('script');
                script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                script.type = 'text/javascript';
                $('head')[0].appendChild(script);


                without jQuery:



                var script = document.createElement('script');
                script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                script.type = 'text/javascript';
                document.getElementsByTagName('head')[0].appendChild(script);


                The script will be added and executed. If this your own script file, an alternative would be to just wrap the code in a function and only call that function during the button click handler.






                share|improve this answer


























                  1












                  1








                  1







                  If you are going to be changing your scripts around, I'd suggest organizing your scripts into files outside of your html and investigating using prototypes. I load almost 100 different html, js, and css fragments so have some utilities that do most of the legwork for me. That said, if you are just wanting to do a simple script add, you can do it like this:



                  Inside your button click event handler do the following.



                  with jQuery: (recommended unless project is quite simple)



                  var script = document.createElement('script');
                  script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                  script.type = 'text/javascript';
                  $('head')[0].appendChild(script);


                  without jQuery:



                  var script = document.createElement('script');
                  script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                  script.type = 'text/javascript';
                  document.getElementsByTagName('head')[0].appendChild(script);


                  The script will be added and executed. If this your own script file, an alternative would be to just wrap the code in a function and only call that function during the button click handler.






                  share|improve this answer













                  If you are going to be changing your scripts around, I'd suggest organizing your scripts into files outside of your html and investigating using prototypes. I load almost 100 different html, js, and css fragments so have some utilities that do most of the legwork for me. That said, if you are just wanting to do a simple script add, you can do it like this:



                  Inside your button click event handler do the following.



                  with jQuery: (recommended unless project is quite simple)



                  var script = document.createElement('script');
                  script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                  script.type = 'text/javascript';
                  $('head')[0].appendChild(script);


                  without jQuery:



                  var script = document.createElement('script');
                  script.src = '{root_url}/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                  script.type = 'text/javascript';
                  document.getElementsByTagName('head')[0].appendChild(script);


                  The script will be added and executed. If this your own script file, an alternative would be to just wrap the code in a function and only call that function during the button click handler.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 17:58









                  Victor Thomas Wilcox Jr.Victor Thomas Wilcox Jr.

                  114




                  114

























                      1














                      this function loads external script, just add this to your condition



                      function loadScript(url, callback) {
                      var script = document.createElement("script")
                      script.type = "text/javascript";
                      if (script.readyState) { //IE
                      script.onreadystatechange = function () {
                      if (script.readyState == "loaded" ||
                      script.readyState == "complete") {
                      script.onreadystatechange = null;
                      callback();
                      }
                      };
                      } else {
                      script.onload = function () {
                      callback();
                      };
                      }
                      script.src = url;
                      document.getElementsByTagName("head")[0].appendChild(script);
                      }


                      here is use example



                      loadScript("[SCRIPT_URL]", function () { 
                      //do something after script loads
                      });





                      share|improve this answer


























                      • Please always link back to the source, or mention the attribution when using code from others.

                        – rlemon
                        Nov 22 '18 at 18:13











                      • @rlemon sorry, i will add on next time, thanks for add

                        – Demian Bibiano
                        Nov 22 '18 at 18:21
















                      1














                      this function loads external script, just add this to your condition



                      function loadScript(url, callback) {
                      var script = document.createElement("script")
                      script.type = "text/javascript";
                      if (script.readyState) { //IE
                      script.onreadystatechange = function () {
                      if (script.readyState == "loaded" ||
                      script.readyState == "complete") {
                      script.onreadystatechange = null;
                      callback();
                      }
                      };
                      } else {
                      script.onload = function () {
                      callback();
                      };
                      }
                      script.src = url;
                      document.getElementsByTagName("head")[0].appendChild(script);
                      }


                      here is use example



                      loadScript("[SCRIPT_URL]", function () { 
                      //do something after script loads
                      });





                      share|improve this answer


























                      • Please always link back to the source, or mention the attribution when using code from others.

                        – rlemon
                        Nov 22 '18 at 18:13











                      • @rlemon sorry, i will add on next time, thanks for add

                        – Demian Bibiano
                        Nov 22 '18 at 18:21














                      1












                      1








                      1







                      this function loads external script, just add this to your condition



                      function loadScript(url, callback) {
                      var script = document.createElement("script")
                      script.type = "text/javascript";
                      if (script.readyState) { //IE
                      script.onreadystatechange = function () {
                      if (script.readyState == "loaded" ||
                      script.readyState == "complete") {
                      script.onreadystatechange = null;
                      callback();
                      }
                      };
                      } else {
                      script.onload = function () {
                      callback();
                      };
                      }
                      script.src = url;
                      document.getElementsByTagName("head")[0].appendChild(script);
                      }


                      here is use example



                      loadScript("[SCRIPT_URL]", function () { 
                      //do something after script loads
                      });





                      share|improve this answer















                      this function loads external script, just add this to your condition



                      function loadScript(url, callback) {
                      var script = document.createElement("script")
                      script.type = "text/javascript";
                      if (script.readyState) { //IE
                      script.onreadystatechange = function () {
                      if (script.readyState == "loaded" ||
                      script.readyState == "complete") {
                      script.onreadystatechange = null;
                      callback();
                      }
                      };
                      } else {
                      script.onload = function () {
                      callback();
                      };
                      }
                      script.src = url;
                      document.getElementsByTagName("head")[0].appendChild(script);
                      }


                      here is use example



                      loadScript("[SCRIPT_URL]", function () { 
                      //do something after script loads
                      });






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 '18 at 18:12









                      rlemon

                      13.7k1077116




                      13.7k1077116










                      answered Nov 22 '18 at 17:55









                      Demian BibianoDemian Bibiano

                      1789




                      1789













                      • Please always link back to the source, or mention the attribution when using code from others.

                        – rlemon
                        Nov 22 '18 at 18:13











                      • @rlemon sorry, i will add on next time, thanks for add

                        – Demian Bibiano
                        Nov 22 '18 at 18:21



















                      • Please always link back to the source, or mention the attribution when using code from others.

                        – rlemon
                        Nov 22 '18 at 18:13











                      • @rlemon sorry, i will add on next time, thanks for add

                        – Demian Bibiano
                        Nov 22 '18 at 18:21

















                      Please always link back to the source, or mention the attribution when using code from others.

                      – rlemon
                      Nov 22 '18 at 18:13





                      Please always link back to the source, or mention the attribution when using code from others.

                      – rlemon
                      Nov 22 '18 at 18:13













                      @rlemon sorry, i will add on next time, thanks for add

                      – Demian Bibiano
                      Nov 22 '18 at 18:21





                      @rlemon sorry, i will add on next time, thanks for add

                      – Demian Bibiano
                      Nov 22 '18 at 18:21











                      0














                      Put the script in a separate .js file. Removed the function myFunc from the script. Next changed the code between the script-tags:



                      <input type="button" class="button-class" onclick="myFunc(this)">
                      <script type="text/javascript">
                      function myFunc(e) {
                      var script = document.createElement('script');
                      script.src = 'http://localhost/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                      script.type = 'text/javascript';
                      $('head')[0].appendChild(script);
                      }
                      </script>


                      Marinetraffic.js



                      var width='100%';   // the width of the embedded map in pixels or percentage 
                      var height='300'; // the height of the embedded map in pixels or percentage
                      var border='1'; // the width of the border around the map (zero means no border)
                      var shownames='true'; // to display ship names on the map (true or false)
                      var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
                      var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
                      var zoom='11'; // the zoom level of the map (values between 2 and 17)
                      var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
                      var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
                      var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
                      // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
                      (function() {

                      if ("https:" == document.location.protocol) {
                      /* secure */
                      var src = 'https://www.marinetraffic.com/';
                      } else {
                      /* unsecure */
                      var src = 'http://www.marinetraffic.com/';
                      }

                      if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
                      window.latitude = 0;
                      window.longitude = 0;
                      }

                      if (typeof window.mtembedcode != "undefined") {
                      var overridenLatLon = '';
                      if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
                      if(window.zoom === undefined){
                      window.zoom = 3;
                      }
                      overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
                      }
                      document.write(
                      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                      );
                      } else {
                      document.write(
                      '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                      );
                      }
                      })()

                      //
                      // EOF
                      //


                      This leaves me a warning in the FireFox developer log:




                      A call to document.write() from an asynchronously-loaded external
                      script was ignored.




                      Any thoughts?






                      share|improve this answer






























                        0














                        Put the script in a separate .js file. Removed the function myFunc from the script. Next changed the code between the script-tags:



                        <input type="button" class="button-class" onclick="myFunc(this)">
                        <script type="text/javascript">
                        function myFunc(e) {
                        var script = document.createElement('script');
                        script.src = 'http://localhost/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                        script.type = 'text/javascript';
                        $('head')[0].appendChild(script);
                        }
                        </script>


                        Marinetraffic.js



                        var width='100%';   // the width of the embedded map in pixels or percentage 
                        var height='300'; // the height of the embedded map in pixels or percentage
                        var border='1'; // the width of the border around the map (zero means no border)
                        var shownames='true'; // to display ship names on the map (true or false)
                        var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
                        var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
                        var zoom='11'; // the zoom level of the map (values between 2 and 17)
                        var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
                        var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
                        var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
                        // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
                        (function() {

                        if ("https:" == document.location.protocol) {
                        /* secure */
                        var src = 'https://www.marinetraffic.com/';
                        } else {
                        /* unsecure */
                        var src = 'http://www.marinetraffic.com/';
                        }

                        if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
                        window.latitude = 0;
                        window.longitude = 0;
                        }

                        if (typeof window.mtembedcode != "undefined") {
                        var overridenLatLon = '';
                        if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
                        if(window.zoom === undefined){
                        window.zoom = 3;
                        }
                        overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
                        }
                        document.write(
                        '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                        );
                        } else {
                        document.write(
                        '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                        );
                        }
                        })()

                        //
                        // EOF
                        //


                        This leaves me a warning in the FireFox developer log:




                        A call to document.write() from an asynchronously-loaded external
                        script was ignored.




                        Any thoughts?






                        share|improve this answer




























                          0












                          0








                          0







                          Put the script in a separate .js file. Removed the function myFunc from the script. Next changed the code between the script-tags:



                          <input type="button" class="button-class" onclick="myFunc(this)">
                          <script type="text/javascript">
                          function myFunc(e) {
                          var script = document.createElement('script');
                          script.src = 'http://localhost/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                          script.type = 'text/javascript';
                          $('head')[0].appendChild(script);
                          }
                          </script>


                          Marinetraffic.js



                          var width='100%';   // the width of the embedded map in pixels or percentage 
                          var height='300'; // the height of the embedded map in pixels or percentage
                          var border='1'; // the width of the border around the map (zero means no border)
                          var shownames='true'; // to display ship names on the map (true or false)
                          var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
                          var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
                          var zoom='11'; // the zoom level of the map (values between 2 and 17)
                          var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
                          var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
                          var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
                          // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
                          (function() {

                          if ("https:" == document.location.protocol) {
                          /* secure */
                          var src = 'https://www.marinetraffic.com/';
                          } else {
                          /* unsecure */
                          var src = 'http://www.marinetraffic.com/';
                          }

                          if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
                          window.latitude = 0;
                          window.longitude = 0;
                          }

                          if (typeof window.mtembedcode != "undefined") {
                          var overridenLatLon = '';
                          if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
                          if(window.zoom === undefined){
                          window.zoom = 3;
                          }
                          overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
                          }
                          document.write(
                          '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                          );
                          } else {
                          document.write(
                          '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                          );
                          }
                          })()

                          //
                          // EOF
                          //


                          This leaves me a warning in the FireFox developer log:




                          A call to document.write() from an asynchronously-loaded external
                          script was ignored.




                          Any thoughts?






                          share|improve this answer















                          Put the script in a separate .js file. Removed the function myFunc from the script. Next changed the code between the script-tags:



                          <input type="button" class="button-class" onclick="myFunc(this)">
                          <script type="text/javascript">
                          function myFunc(e) {
                          var script = document.createElement('script');
                          script.src = 'http://localhost/assets/src/cmsms_uisge-beatha/js/marineTraffic.js';
                          script.type = 'text/javascript';
                          $('head')[0].appendChild(script);
                          }
                          </script>


                          Marinetraffic.js



                          var width='100%';   // the width of the embedded map in pixels or percentage 
                          var height='300'; // the height of the embedded map in pixels or percentage
                          var border='1'; // the width of the border around the map (zero means no border)
                          var shownames='true'; // to display ship names on the map (true or false)
                          var latitude='51.7143'; // the latitude of the center of the map, in decimal degrees
                          var longitude='04.0889'; // the longitude of the center of the map, in decimal degrees
                          var zoom='11'; // the zoom level of the map (values between 2 and 17)
                          var maptype='0'; // use 0 for Normal Map, 1 for Satellite, 2 for OpenStreetMap
                          var trackvessel='' //244770624'; MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
                          var fleet=''; // the registered email address of a user-defined fleet (user's default fleet is used)
                          // Read more at http://www.marinetraffic.com/en/p/embed-map#6YXCVvOUaBxYHgoT.99
                          (function() {

                          if ("https:" == document.location.protocol) {
                          /* secure */
                          var src = 'https://www.marinetraffic.com/';
                          } else {
                          /* unsecure */
                          var src = 'http://www.marinetraffic.com/';
                          }

                          if ((window.latitude === undefined) && (window.longitude === undefined) && ( (window.fleet !== undefined && window.fleet != "") || (window.fleet_id !== undefined && window.fleet_id != ""))) {
                          window.latitude = 0;
                          window.longitude = 0;
                          }

                          if (typeof window.mtembedcode != "undefined") {
                          var overridenLatLon = '';
                          if(window.latitude !== undefined && window.latitude != '' && window.longitude !== undefined && window.longitude != ''){
                          if(window.zoom === undefined){
                          window.zoom = 3;
                          }
                          overridenLatLon = '/zoom:' + ((window.zoom === undefined) ? '' : zoom) + '/centery:' + ((window.latitude === undefined) ? '' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '' : longitude);
                          }
                          document.write(
                          '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/customembed' + overridenLatLon + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + ((window.trackimo !== undefined) ? '/trackimo:' + window.trackimo : '') + '/mtembedcode:' + window.mtembedcode + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                          );
                          } else {
                          document.write(
                          '<iframe name="marinetraffic" id="marinetraffic"' + ' width="' + ((window.width === undefined) ? '550' : width) + '"' + ' height="' + ((window.height === undefined) ? '300' : height) + '"' + ' scrolling="no" frameborder="' + ((window.border === undefined) ? '0' : border) + '"' + ' src="' + src + ((window.language === undefined) ? 'en' : language) + '/ais/embed' + '/zoom:' + ((window.zoom === undefined) ? '3' : zoom) + '/centery:' + ((window.latitude === undefined) ? '36' : latitude) + '/centerx:' + ((window.longitude === undefined) ? '23' : longitude) + '/maptype:' + ((window.maptype === undefined) ? '4' : maptype) + '/shownames:' + ((window.shownames === undefined) ? 'false' : shownames) + '/mmsi:' + ((window.trackvessel === undefined) ? '0' : trackvessel) + '/shipid:' + ((window.trackshipid === undefined) ? '0' : trackshipid) + '/fleet:' + ((window.fleet === undefined) ? '' : fleet) + '/fleet_id:' + ((window.fleet_id === undefined) ? '' : fleet_id) + '/vtypes:' + ((window.vtypes === undefined) ? '' : vtypes) + '/showmenu:' + ((window.showmenu === undefined) ? '' : showmenu) + '/remember:' + ((window.remember === undefined) ? 'false' : remember) + '">Browser does not support embedded objects.<br/>Visit directly <a href="http://www.marinetraffic.com/">www.marinetraffic.com</a>' + '</iframe>n'
                          );
                          }
                          })()

                          //
                          // EOF
                          //


                          This leaves me a warning in the FireFox developer log:




                          A call to document.write() from an asynchronously-loaded external
                          script was ignored.




                          Any thoughts?







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 '18 at 13:59

























                          answered Nov 22 '18 at 21:25









                          user2037412user2037412

                          528




                          528






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435910%2fload-external-javascript-when-button-is-clicked%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              Origin of the phrase “under your belt”?