Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据从输入的url接收到javascript网页_Javascript_Html - Fatal编程技术网

将数据从输入的url接收到javascript网页

将数据从输入的url接收到javascript网页,javascript,html,Javascript,Html,我有一个网页提醒我该网站的当前.href。然而,我想能够通过一个简短的变量进入网页时,输入域名,以便我以后可以使用在js 例如,进入网站localhost时输入:localhost/HelloWorld应提醒“HelloWorld” 这可能吗 这是我的html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Get Current URL

我有一个网页提醒我该网站的当前.href。然而,我想能够通过一个简短的变量进入网页时,输入域名,以便我以后可以使用在js

例如,进入网站localhost时输入:localhost/HelloWorld应提醒“HelloWorld”

这可能吗

这是我的html代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Current URL in JavaScript</title>
</head>
<body>
    <script>
    function getURL() {
        alert("The URL of this page is: " + window.location.href);
    }
    </script>

    <button type="button" onclick="getURL();">Get Page URL</button>
</body>
</html>

获取JavaScript中的当前URL
函数getURL(){
警报(“此页面的URL为:“+window.location.href”);
}
获取页面URL

只需使用
路径名
属性就可以了。对象包含其中的所有主机和路径

<script>
function getURLPath() {
    alert("The URL path of this page is: " + window.location.pathname);
}
</script>

函数getURLPath(){
警报(“此页面的URL路径为:“+window.location.pathname”);
}

只需使用
路径名
属性就可以了。对象包含其中的所有主机和路径

<script>
function getURLPath() {
    alert("The URL path of this page is: " + window.location.pathname);
}
</script>

函数getURLPath(){
警报(“此页面的URL路径为:“+window.location.pathname”);
}

假设地址是
https://localhost/HelloWorld
,请尝试
window.location.pathname.split('/')[1]

pathname
是域
localhost
之后的字符串
/HelloWorld
。使用
split
可以将其拆分为多个部分(在本例中为两个)。第一部分将始终是空字符串。在本例中,第二部分是字符串
HelloWorld


为此,web服务器必须在所有路径上提供index.html。这是节点的标准行为。否则,您将需要编辑web服务器的配置文件。

假设地址为
https://localhost/HelloWorld
,请尝试
window.location.pathname.split('/')[1]

pathname
是域
localhost
之后的字符串
/HelloWorld
。使用
split
可以将其拆分为多个部分(在本例中为两个)。第一部分将始终是空字符串。在本例中,第二部分是字符串
HelloWorld


为此,web服务器必须在所有路径上提供index.html。这是节点的标准行为。否则,您将需要编辑web服务器的配置文件。

如果转到
localhost/HelloWorld
服务器将尝试查找不存在的HelloWorld页面。相反,向页面传递参数的正确方法是通过,例如:
localhost?q=HelloWorld
。您还可以使用自定义路由器将
localhost/HelloWorld
重定向到
localhost?q=HelloWorld
,具体取决于您的服务器,例如使用
.htaccess
文件(如果使用Apache)


您可以使用
window.location.search
或转换为URL对象并使用来检索参数。

如果转到
localhost/HelloWorld
服务器将尝试查找不存在的HelloWorld页面。相反,向页面传递参数的正确方法是通过,例如:
localhost?q=HelloWorld
。您还可以使用自定义路由器将
localhost/HelloWorld
重定向到
localhost?q=HelloWorld
,具体取决于您的服务器,例如使用
.htaccess
文件(如果使用Apache)


您可以使用
window.location.search
或转换为URL对象并使用。

来检索参数,这正是我要查找的!成功了。谢谢你,伙计!正是我要找的!成功了。谢谢你,伙计!