首页 > 解决方案 > javascript changed my link address in string

问题描述

I have a link from google, and I use this in iframe html tag. I want to change a part of this link but I get some error. seems %20 is the problem in the link. How can I take this link without any change?

<script>
    document.getElementById('mapcanvas1').src = "https://maps.google.com/maps?q=37.474203,%2049.446669&amp;t=&amp;z=10&amp;ie=UTF8&amp;iwloc=&amp;output=embed";
</script>

标签: javascript

解决方案


首先,您遇到了 URL 中的编码问题。通过以下方式修复:

  • 更改%20为“空格”(实际空格,而不是单词空格);
  • 更改&amp;&;

然后变成如下:

<script type="text/javascript"> document.getElementById('mapcanvas1').src = "https://maps.google.com/maps?q=37.474203,49.446669&t=&z=10&ie=UTF8&iwloc=&output=embed"; </script>

其次,坐标值可以处理如下:

  • 对于地址中的新变量,只需&在末尾加上变量名=和值。示例:...example.com/?q=...&newVar=value;

  • 要更改坐标,请更改q=37.474203,49.446669为实际坐标。示例:q=" + lat + "," + lon + "beinglatlatitude 和 longitudelon的坐标变量;

然后变成如下:

<script type="text/javascript"> document.getElementById('mapcanvas1').src = "https://maps.google.com/maps?q=" + lat + "," + lon + "&t=&z=10&ie=UTF8&iwloc=&output=embed"; </script>

注意:链接地址中的第一个变量以?和非启动&。只有第二个和以后的使用&.


推荐阅读