从请求URL获取哈希参数 [英] Getting hash parameters from request url

查看:922
本文介绍了从请求URL获取哈希参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的网址 - http://www.coolsite.com/daily- plan /#id = 1
解析该字符串并读取哈希值(#id =之后的值)的最简单方法是什么?
谢谢

I have such url - http://www.coolsite.com/daily-plan/#id=1 What the easiest way to parse that string and read a hash value (the value after #id=)? Thank you

推荐答案

在客户端(即从JavaScript),您可以检查窗口。 location.hash 获取哈希值。在服务器端,一般答案是不可能,因为哈希不会在请求中发送到服务器。

On client side (i.e. from JavaScript) you can check window.location.hash to get hash. On server side, general answer is 'it is impossible' since hash is not sent in request to server.

更新:我可能误解了题。我的答案是关于如何在请求处理期间在浏览器或服务器端代码中获取URL的哈希部分,而不是字符串处理。

Upd: I maybe misunderstood the question. My answer is about how to get hash part of url either in browser or in server side code during request processing, not about string processing.

Upd2:回答此处的评论,因为它不适合评论。

Upd2: Answer to comment here because it doesn't fit in comment.

当用户点击您的导航链接时,它是如何工作的?

How does it work when user clicks on your navigational links?

我假设哈希被更改,相应的内容通过来自Web服务或REST的AJAX请求下载。

I assume hash is changed and corresponding content is downloaded via AJAX request from web service or REST.

例如,如果您的用户在其浏览器中有URL www.example.com,则此页面会显示产品类别列表。用户点击一个类别,URL更改为www.example.com/#id=5,该类别(ID = 5)的产品将通过AJAX下载并显示在页面上。没有回发,只有部分页面刷新。

For example if your user has URL www.example.com in his browser and this page shows a list of product categories. User clicks one category and URL changes to www.example.com/#id=5 and products from that category(with ID=5) are downloaded via AJAX and shown on the page. No postback, only partial page refresh.

这是否接近您的情况?

现在您希望用户粘贴/输入www.example.com /#id = 5直接在浏览器地址栏中直接转到该类别中的产品列表。

Now you want user to paste/enter www.example.com/#id=5 directly in the browser address bar and go directly to list of products in that category.

但浏览器 /#id = 5未通过请求发送到服务器,因此无法在服务器上获取该值因为浏览器决定不发送这些数据,而你在服务器端没有它,所以你无能为力。

But /#id=5 is not sent to server with request by the browser, so there is no way to get that value on server side, and you can do nothing about it since it is the browser decided not to send this data and you don't have it on server side.

在我们的项目中我们当服务器只返回公共页面代码/ html时使用解决方案,即页眉,页脚,没有页面的主/中心部分。然后有一个JavaScript代码,它在加载这个常见的HTML之后立即执行。它需要 window.location.hash 并通过AJAX将其发送到Web服务,Web服务返回页面主要部分的内容(HTML)。

In our project we use solution when server returns only common page code/html, i.e. header, footer, without main/center part of the page. Then there is a JavaScript code which executes right after this common HTML loaded. It takes window.location.hash and sends it to web service via AJAX and web service returns content (HTML) for the main part of the page.

这篇关于从请求URL获取哈希参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆