javascript - 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&t=&z=10&ie=UTF8&iwloc=&output=embed";
</script>
解决方案
首先,您遇到了 URL 中的编码问题。通过以下方式修复:
- 更改
%20
为“空格”(实际空格,而不是单词空格); - 更改
&
为&
;
然后变成如下:
<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 + "
beinglat
和latitude 和 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>
注意:链接地址中的第一个变量以?
和非启动&
。只有第二个和以后的使用&
.
推荐阅读
- php - 无法在 php 中使用 sqlsrv 执行存储过程
- jquery - 为什么我在不使用任何 gem 或 js 库的情况下在 Rails 中接收 jquery 访问?
- mongodb - 如何在 Kubernetes 上部署的 mongodb 中持久化数据
- java - 在 Java Map 中为每个键分配多个值
- dynamodb-queries - 在不使用 SCAN 的情况下获取与非关键属性 dynamoDB 匹配的所有记录
- javascript - 如果 URL 仅包含 .co,如何显示验证消息
- javascript - 收到错误 this.setState 不是类组件中的函数
- java - 我无法映射模型和 DTO 无法执行语句;SQL [n/a] 约束 [null]
- visual-studio-code - 如何让 VS Code 格式化空行?
- javascript - 在一个“.map”中为数组的每个元素获取多个对象