doudong4532 2018-09-11 04:39
浏览 103

查询字符串变量自动添加到url

I am pretty much new to programming and just getting my hands dirty. I am trying to show something to the user depending upon the length of the URL. Following is the code

<!DOCTYPE HTML>
<html>

<head>
    <title>Random</title>
    <meta charset="utf-8" />
</head>

<body id="main" style="display:none">
    <h1>
        Paste something
    </h1>
    <form action="abcd.php" method="post">
        <input type="text" name="URL" placeholder="random url" id="something" required>
        <input type="submit" value="do something">
    </form>
</body>
<script>
    if (window.location.href.length - 1 > 35) {
        alert("redirect");
    } else {
        alert("show");
    }
</script>

</html>

In my case, the minimum length of the URL is 35 characters. However, when I add the URL that long for the first time the "redirect" alert shows up instead of showing the "show" alert. It runs fine the second time. And after I delete the cache, the whole story repeats.

PS: query string variable I get added on its own. For example, when I hit domain.com its written domain.com/?i=1 in the address bar on its own(Only when I hit the URL for the first time after deleting cache).

How can I make it work in the first go? Any help appreciated.

  • 写回答

1条回答 默认 最新

  • ds9567 2018-09-11 05:35
    关注

    I don't know why query string is added the first time page loading. Use below example to remove query string from the URL and calculate the length of the URL without the query string.

    <script>
      var url=window.location.protocol;
          url +="//"+window.location.host;
          url +=window.location.pathname;
         if(url.length-1 > 35){
           alert("redirect");
         }else{
           alert("show");
         }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集