Getting a Parameter on The URL

时间:2022-06-11 10:24:21

It's common to use URL parameters to show dynamic information, just like here on *:

使用URL参数来显示动态信息是很常见的,就像在*上一样:

http://*.com/questions/1183842

But would that be possible when you're without any language like PHP or others? Is there any way to get that value using JavaScript and capture it inside a variable to present it like this:

但是当你没有PHP或其他语言时,这是否可能?有没有办法使用JavaScript获取该值并将其捕获到变量中以显示如下:

<h1>param_variable</h1>

2 个解决方案

#1


5  

window.location.href contains the current URL. With some string manipulations you can grab anything you want from it.

window.location.href包含当前的URL。通过一些字符串操作,您可以从中获取任何内容。

In this case:

在这种情况下:

var url = window.location.href;
var id = url.split('/')[4];
alert(id);

Will alert

4479268

#2


0  

In JavaScript you can get the current page's url like this:

在JavaScript中,您可以像这样获取当前页面的URL:

   var url = location.href;

You may then need to parse it your self to find the parameter that you are looking for.

然后,您可能需要自己解析它以找到您要查找的参数。

#1


5  

window.location.href contains the current URL. With some string manipulations you can grab anything you want from it.

window.location.href包含当前的URL。通过一些字符串操作,您可以从中获取任何内容。

In this case:

在这种情况下:

var url = window.location.href;
var id = url.split('/')[4];
alert(id);

Will alert

4479268

#2


0  

In JavaScript you can get the current page's url like this:

在JavaScript中,您可以像这样获取当前页面的URL:

   var url = location.href;

You may then need to parse it your self to find the parameter that you are looking for.

然后,您可能需要自己解析它以找到您要查找的参数。