window.location.url Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10783322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:54:30  来源:igfitidea点击:

window.location.url Javascript

javascript

提问by Sarfraz

I'm splitting the current url into pieces but I'm doing something wrong with this part to get the current url. How can I solve this?

我正在将当前的 url 分成几部分,但我在这部分做错了以获取当前的 url。我该如何解决这个问题?

var url = window.location.url; 

I'm trying to get the current url from the page. This is my function

我正在尝试从页面中获取当前 url。这是我的功能

function split(){

    var url = window.location.url;     // This part is not correct
    var firstSplit = url.split('?')[1];

    var name = firstSplit.split('&')[0];
    var age = firstSplit.split('&')[1];

    var parName = name.split('=')[0];
    var nameName = name.split('=')[1];

    var parAge = age.split('=')[0];
    var ageAge = age.split('=')[1];

    document.getElementById("nameId").innerHTML=naamName;
    document.getElementById("ageId").innerHTML=leeftijdAge;
}

回答by Sarfraz

Use href:

使用href

window.location.href

For example running it on current SO page gives:

例如在当前 SO 页面上运行它会给出:

console.log(window.location.href);

this:

这个:

http://stackoverflow.com/questions/10783322/window-location-url-javascript

回答by Joseph Oster

Add the following script to a page; it will display all the properties of the page's url:

将以下脚本添加到页面;它将显示页面 url 的所有属性:

document.write("<li>location.href = " + location.href);
document.write("<li>location.protocol = " + location.protocol);
document.write("<li>location.host = " + location.host);
document.write("<li>location.hostname= " + location.hostname);
document.write("<li>location.port = " + location.port);
document.write("<li>location.pathname = " + location.pathname);
document.write("<li>location.hash = " + location.hash);
document.write("<li>location.search = " + location.search);

better answer found here: https://gist.github.com/jlong/2428561

更好的答案在这里找到:https: //gist.github.com/jlong​​/2428561

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.host;     // => "example.com:3000"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.hash;     // => "#hash"
parser.search;   // => "?search=test"

回答by georgebrock

You can access different parts of the URL directly by using different properties on window.location.

您可以通过使用 上的不同属性直接访问 URL 的不同部分window.location

The full URL is window.location.href, but you seem to be more interested in the query string (the part after the question mark) which you can access with window.location.search

完整的 URL 是window.location.href,但您似乎对可以使用的查询字符串(问号后面的部分)更感兴趣window.location.search

A list of other properties is can be found in the MDN article about window.location.

其他属性的列表可以在MDN 文章中window.location找到。

回答by Er. Anurag Jain

Try

尝试

var url = window.location.href;

Or

或者

var url = document.URL;

and I think Please use function name something different then split because it is a function of PHP and may be reason of problem.

我认为请使用不同的函数名称然后拆分,因为它是 PHP 的一个函数,可能是问题的原因。

thanks

谢谢

回答by andre mcgruder

I'm not exactly sure what yo are trying to do after you break the URL into parts but here is how you can get the current URL and put them into variables. You can put them back together however you like latter. Source: [http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/][1]

在将 URL 分成多个部分后,我不确定您要尝试做什么,但这里是获取当前 URL 并将它们放入变量的方法。您可以将它们重新组合在一起,但您喜欢后者。来源:[http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/][1]

var urlNow = window.location.pathname.split('/');

var itNow0 = urlNow[0];
var itNow1 = urlNow[1];
var itNow2 = urlNow[2];
var itNow3 = urlNow[3];

alert(itNow0+ ' ' +itNow1+ ' ' +itNow2+ ' ' +itNow3);