【问题标题】:Where can I try an my code with an input that actually replaces the search bar value?我在哪里可以尝试使用实际替换搜索栏值的输入的代码?
【发布时间】:2021-05-06 05:12:47
【问题描述】:

我有这个类似 Netflix 浏览器搜索功能的代码:

let url = location.href;
  localStorage.setItem("url",url);
    function modURL() {
      if (typeof (history.pushState) != "undefined") {
        //get the url
        let newURL = localStorage.getItem("url")+'/?q='+document.getElementById('append').value;
        let title = document.getElementById('append').value;
        let obj = { Title: title, Url: newURL };

        console.log("url ", newURL);
        history.pushState(obj, obj.Title, obj.Url);
      } else {
        alert("Browser does not support HTML5.");
      }
    }
<label>
  End URL<input type='text' onInput='modURL()' id='append'>
<label>

我想测试它是否真的替换了 URL。我试过fiddle,日志输出的URL是正确的,但是有没有一个在线编辑器可以让你替换搜索栏?

【问题讨论】:

  • 您无法在您的计算机上启动本地服务器?
  • @JohnTyner。我可以。我有。我很快就在 django 服务器上进行了尝试。它适用于模板。但我试图避免当地的发展。无论如何把它作为答案,让我投票。
  • 出于好奇,您为什么要避免本地部署
  • 我偷懒了。我首先是出于好奇和answer 这个question 这样做的。
  • 如果有在线编辑器能够处理这个问题还是很高兴的。

标签: javascript html url pushstate


【解决方案1】:

Stackblitz 做得很好。见here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多