【问题标题】:Insert current url in textfield on page load在页面加载时在文本字段中插入当前 url
【发布时间】:2017-01-13 23:43:45
【问题描述】:

我希望我的 URL 在页面加载时显示在文本字段中。 我试过了,但没有用:

function getUrl() {
  var url = document.URL;
  document.getElementById("textfield").value = url;
}
<input type="text" name="textfield" id="textfield" readonly="true" onload="getUrl()" value="">

【问题讨论】:

  • 尝试:var url = window.location.href;

标签: javascript


【解决方案1】:

The onload attribute will not work on an input element。相反,您可以在窗口上设置一个侦听器以监视页面何时“加载”。加载完毕后调用函数:

function getUrl() {
  var url = document.URL;
  document.getElementById("textfield").value = url;
}

window.addEventListener('load', getUrl);
<input type="text" name="textfield" id="textfield" readonly="true" value="">

【讨论】:

  • 工作,谢谢:) 你能解释一下为什么我收到了反对票吗?只是为了以后的帖子,所以我知道我可以做得更好。提前谢谢你。
  • @TomiG,老实说我不太确定。我没有看到任何具体的原因。一定是人们对新用户过于挑剔。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多