从URL中获取用户名?

huangapple go评论57阅读模式
英文:

How To Get UserName from url?

问题

我想从像"example.com/u/AbcUser"这样的URL中获取用户名。
我知道我们可以通过使用HTTP GET请求来获取它,但我想创建一个常规的最新站点。

是否可以使用Vanilla JS和HTML来实现这个目标?

英文:

I want a username from a URL like "example.com/u/AbcUser".
I know we can get it by using HTTP GET request, But I want to create a regular latest site

is it possible with Vanilla JS and HTML ??

答案1

得分: 1

const url = window.location.pathname
const userName = url.split("/")[2]

英文:
const url = window.location.pathname
const userName = url.split("/")[2]

答案2

得分: 0

<h1 id="wel" style="text-align: center; size: 25;"></h1>
function getUserName(){
    var url = location.href;
    var spliting = url.split("?");

    var splitUser = spliting[1].split("&");

    var userNam = splitUser[0].split("=");

    return document.getElementById('wel').innerHTML = "Welcome :  &nbsp; "+userNam[0];
}

getUserName();
英文:
    &lt;h1 id=&quot;wel&quot; style=&quot;text-align: center; size: 25;&quot;&gt; &lt;/h1&gt;
`enter code here` function getUserName(){
    var url = location.href;
    var spliting =url.split(&quot;?&quot;);

    var splitUser = spliting[1].split(&quot;&amp;&quot;);

    var userNam= splitUser[0].split(&quot;=&quot;);


   return document.getElementById(&#39;wel&#39;).innerHTML = &quot;Welcome :  &amp;nbsp; &quot;+userNam[0];

    }

    getUserName();

huangapple
  • 本文由 发表于 2023年5月11日 13:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定