简体   繁体   中英

javascript runtime code edit

I want to add some code inside javascript during runtime, these code is dependent on the file where some data is being written dynamically. I want that datas to be written inside my javascript code. Any help would be appreciated thanks :)

  function initialize() {
                  var myLatLng = new google.maps.LatLng(0, -180);

                  var map = new google.maps.Map(document.getElementById("map_canvas"),
  myOptions);
                  var flightPlanCoordinates = [
                                        new google.maps.LatLng(37.772323, -122.214897),
                                        new google.maps.LatLng(21.291982, -157.821856),
                                        new google.maps.LatLng(-18.142599, 178.431)
                                               ];

                  flightPath.setMap(map);
            } 

I want to set flightPathCoordinates dynamically using a loop....

If you meant loading data dynamically, there is what you call AJAX to dynamically load data into your program.

If you meant dynamically loading parts of usable, executable scripts, try the module pattern and dynamic module loading like how RequireJS does it.

In addition to what @Joseph has provided, and since this is tagged with asp.net, you can do the classic <%= ....%> inline code style of asp.net. You can make any part of your Javascript "dynamic" - Of course your script will also be inline (not some separate file) to keep things simple.

You can also dynamically write the entire, or parts of, the script you want to inject with ASP.Net ClientScript and more....

There are different patterns to do this - Joseph's Ajax pattern will likely require you to still exchange data with ASP.Net backend (I think that's why you're tagging your question in ASP.Net). The strength of this approach is that you minimize postbacks (even to none - the front end can entirely be HTML + JS and its just the datasource that has anything to do with ASP.Net).

Which you choose is entirely up to you and your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM