最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Splitting page into JSCSSHTML - Stack Overflow

programmeradmin8浏览0评论

I would like to break this code into one JS, CSS and HTML file each. How could I do that?

<!DOCTYPE html>
<html>
<head>
  <title>Simple Map</title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">

  <style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
  </style>

  <script src=".exp"></script>

  <script>
  var map;
  function initialize() {
    var mapOptions = {
      zoom: 8,
      center: new google.maps.LatLng(-34.397, 150.644)
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
  </script>

</head>
<body>
  <div id="map-canvas"></div>
</body>
</html>

I would like to break this code into one JS, CSS and HTML file each. How could I do that?

<!DOCTYPE html>
<html>
<head>
  <title>Simple Map</title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">

  <style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
  </style>

  <script src="https://maps.googleapis./maps/api/js?v=3.exp"></script>

  <script>
  var map;
  function initialize() {
    var mapOptions = {
      zoom: 8,
      center: new google.maps.LatLng(-34.397, 150.644)
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
  </script>

</head>
<body>
  <div id="map-canvas"></div>
</body>
</html>
Share Improve this question edited Apr 17, 2015 at 15:16 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Apr 17, 2015 at 14:50 OLuOLu 771 silver badge5 bronze badges 1
  • Everything between the <style> and '</style>' tags (not including the tags) goes into your css file. Everything between '<script>' and '</script>' go into your js file. Then replace the style tag with the link to the css file and replace the script tag with a script tag with the source set to the javascript file. – Grax32 Commented Apr 17, 2015 at 14:55
Add a ment  | 

1 Answer 1

Reset to default 9

You can move the Javascript (contents between scripts tags) to a separate file, for example

<script src="scripts/myscripts.js"></script>

Similarly you can move the CSS (contents between style tags and embed it like this:

<link rel="stylesheet" href="my.css" />

So your Javascript file would look like this:

var map;
function initialize() {
 var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
 };
 map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);
} 

google.maps.event.addDomListener(window, 'load', initialize);

and your CSS file like this:

html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
发布评论

评论列表(0)

  1. 暂无评论