简体   繁体   English

如何在HTML标头中加载可选的JavaScript

[英]How do I load a selectable javascript in html header

I'm looking for a way to select and load a javascript from the html file arguments. 我正在寻找一种从html文件参数中选择并加载javascript的方法。 The html file is called as follows: html文件的调用如下:

OSM_Map.html?year=2017 or OSM_Map.html?year=2018 OSM_Map.html?year = 2017或OSM_Map.html?year = 2018

In the OSM_Map.html file there is the following code in the header: 在OSM_Map.html文件的标头中包含以下代码:

<head>
.....
<script language="JavaScript" type="text/javascript" src="LatLonDB_2017.js"></script>
<script language="JavaScript" type="text/javascript" src="LatLonDB_2018.js"></script>
....
</head>

There is no problem to get the year argument from the argument list, but how can I load depending on the year argument just one of these .js files? 从参数列表中获取year参数是没有问题的,但是如何根据year参数加载这些.js文件之一?

As somebody said, "yes, you can": 正如有人说的:“是的,您可以”:

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/loadjs/3.5.5/loadjs.min.js"></script>
   <script>
      var myparameter = new URL(location.href).searchParams.get("year");
      loadjs( "LatLonDB_" + myparameter +".js" );
    </script>

</head>
<body>
    <h1>Titulo</h1>
</body>
</html>

Been the URL something like: http://test.html?year=2018 网址是这样的http://test.html?year = 2018

BUT! 但! not sure if this will work in every browser.... the "searchParams" is not universally compatible. 不知道这是否将在每种浏览器中都有效。...“ searchParams”不是通用兼容的。

Thanks to @spencer.sm in this question How to get the value from the GET parameters? 感谢这个问题中的@ spencer.sm 如何从GET参数中获取值?

and of course, loadJS function. 当然还有loadJS函数。

The LatLonDB_xxxx.js script still doesn't load. LatLonDB_xxxx.js脚本仍然无法加载。 I'm not sure why not. 我不确定为什么不这样做。 Cannot you load a .js file from another directory than where the .html file is? 您无法从.html文件所在的其他目录中加载.js文件吗? Otherwise the load may be too late. 否则负载可能为时已晚。 Scripts following the LatLonDB_xxxx.js script use this DB. LatLonDB_xxxx.js脚本之后的脚本使用此数据库。

The original code is like this: 原始代码如下:

<html>
<head>
  ...
  <script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_20xx.js"></script>
  <script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_utils.js"></script>
  <script language="JavaScript" type="text/javascript" ...more scripts using the LatLonDB_20xx.js></script>
  ...
</head>
...
</html>

The intention is to replace the 20xx by the correct year: 2000, 2001, etc. There is no problem to get the correct year from the query parameters. 目的是将20xx替换为正确的年份:2000、2001等。从查询参数中获取正确的年份没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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