English 中文(简体)
用 Java本书写的URL hash/fragment识别器
原标题:Parsing URL hash/fragment identifier with JavaScript

寻找途径,将URL的 has/红外的主要乳制品归入与Javaend/JQuery的物体/联系阵列

最佳回答
问题回答

URLSearchParams。 它在主要浏览器和服务器—— Java路台运行时间得到充分支持,执行。 WHATWG URL Standard。 Browser保险:https://caniuse.com/urlsearchparams

阅读目前URL的钥匙(假设你在浏览器UA):

// window.location.hash = "#any_hash_key=any_value"

const parsedHash = new URLSearchParams(
  window.location.hash.substring(1) // any_hash_key=any_value
);

console.log(parsedHash.get("any_hash_key")); // any_value

阅读了任意的URL(https://developer.mozilla.org/en-US/docs/Web/API/URL/URL# Tenions” rel=“nofollow noreferer”>,在URL不有效的情况下,URL Constructionor可能提供的例外情况:

const url = new URL("https://example.com#foo=bar&baz=qux&val=val+has+spaces");

const parsedHash = new URLSearchParams(
  url.hash.substring(1) // foo=bar&baz=qux&val=val+has+spaces
);

console.log(parsedHash.get("baz")); // qux
console.log(parsedHash.get("val")); // val has spaces

查阅上文链接到的Catherdocs I。

只是 Java字:

var hash = window.location.hash.substr(1);

var result = hash.split( & ).reduce(function (result, item) {
    var parts = item.split( = );
    result[parts[0]] = parts[1];
    return result;
}, {});

......

<代码>{:2012-01-05 至:2013-01 }

我正在使用jQuery URL Parser

我正在通过一组答案来研究这一问题,并用一个字典(<><<>reduce/code>)把这些问题合在一起:

const hashObj = location.hash.replace( # ,   ).split( & ).reduce((prev, item) => Object.assign({[item.split( = )[0]]: item.split( = )[1]}, prev), {});

这段话显然还有许多进展。 声明可以改写:

const hashObj = location.hash.replace( # ,   ).split( & ).reduce((prev, item) => {
  return Object.assign({[item.split( = )[0]]: item.split( = )[1]}, prev);
}, {});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签