【问题标题】:Hide a questiomark in url?在网址中隐藏问号?
【发布时间】:2018-11-18 03:50:46
【问题描述】:

我的 url 看起来像 http://localhost/dashboard/index.php?id=1,这样我就可以根据使用情况传递 url 中的值并相应地切换仪表板。我想要的只是 url 应该像 http://localhost/dashboard/index.php 甚至 http://localhost/dashboard/index.php/1 一样可见。我想隐藏或替换一个 url 字符串(为了可见性)而不是重定向我尝试使用 htaccess。我们可以使用 JavaScript 做到这一点吗??

【问题讨论】:

  • 你需要一些服务器端的魔法,更改 dicument.location.search 属性会重新加载页面
  • 可以使用htaccess实现

标签: javascript php url replace


【解决方案1】:
var urlStr = 'http://localhost/dashboard/index.php?id=1';
var nextURL = urlStr.replace("?", "");
alert(nextURL);

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
【解决方案2】:

您可以使用 AJAX 从 php 文件中获取数据并在浏览器上显示内容。 这将避免重定向,并且 url 也将保持干净。

还没有尝试过,但你可以这样做:

<!DOCTYPE html>
<html>
<body>
<p><span id="dashboardView"></span></p> 
<span class="btn" val="1" onclick="showDashboard(this)">Dashboard 1</span>

<script>
function showDashboard(elem) {
  var xhttp;
  var val = elem.getAttribute("val");

  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("dashboardView").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "dashboard/index.php?id="+val, true);
  xhttp.send();   
}
</script>

</body>
</html>

【讨论】:

    【解决方案3】:

    您可以在浏览器历史记录 javascript 中推送状态。这将更改您当前页面的网址,而不会重定向到新网址。在页面加载事件中尝试以下代码。

    history.pushState("", "", "1");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多