Vanilla JS中的Routing(?)

时间:2019-05-28 07:03:04

标签: javascript routing

我需要我的网站根据我网站的URL中的查询(例如www.website.com/index.php?country=FR)以某种语言显示信息。我该如何使用香草JS而不是React / Angular?

我的方法:

1)JS识别URL中的查询(在本例中为“ country = FR”),然后附加一个js文件,该文件中包含由变量定义的必要法语单词。 2)HTML文件中我的脚本标签中的JS,在其中添加了带有模板文字的主页标记文本。 3)

我不知道浏览器是无法获取语言文件本身还是其变量。目前,它不呈现任何内容。

    <img src="../../assets/floor2.jpg" usemap="#floor2Map">    
        <map name="floor2Map">
            <area shape="poly" coords="210,340,216,234,312,236,323,323,317,346" matTooltip="Info about the Device" >
            <div id='pin-1' class="box" >
            <div class="pin-text">
                <h4>My Device</h3>
             </div>
        </div>
        </map>


In CSS file

.box:hover > .pin-text {
    display: block;
  }

  .box {
    width:8%;
    height:8%;
    background-image:   url('http://www.clker.com/cliparts/W/0/g/a/W/E/map-pin-red.svg');
    background-position: top;
    background-repeat: no-repeat;
    background-size: contain;
    position: absolute; 
  }

  .pin-text {
    position: absolute;
    top:50%;
    transform:translateY(-50%);
    left:75%;
    white-space:nowrap;
    display: none;
  }

1 个答案:

答案 0 :(得分:0)

在main.js文件中,您正在使用domain.includes,它仅返回域名,而不返回整个URL。您可以为此使用window.location.href.includes。

代替:domain.includes(“ country = FR”); 试试:window.location.href.includes(“ country = FR”);

相关问题