can't get the hash of a url

2,385 views
Skip to first unread message

Geuis

unread,
Aug 14, 2010, 12:38:49 AM8/14/10
to nodejs
I'm trying to get the hash of a url that's being passed in to my
simple http server, but the only params that appear to be in the uri
are the path and href. The following is my code:

var http = require('http'),
url = require('url'),
path = require('path');

http.createServer( function(request, response) {

var uri = url.parse(request.url);

console.log( uri.hash );

response.writeHead(200, {'Content-Type': 'text/plain'});
response.end( uri.href );

}).listen(8080);

On the terminal, no uri.hash is being output, just undefined.

Marak Squires

unread,
Aug 14, 2010, 1:11:04 AM8/14/10
to nod...@googlegroups.com
hashes dont get passed from the browser, sorry.


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


markc

unread,
Aug 14, 2010, 1:30:42 AM8/14/10
to nodejs
That probably is true in general but php is nice enough to provide the
hash fragment...

~ cat fragment.php
<?php
$url = 'http://u:p...@example.com/path?arg=1#anchor';
print_r(parse_url($url));
?>

~ php fragment.php
Array
(
[scheme] => http
[host] => example.com
[user] => u
[pass] => pw
[path] => /path
[query] => arg=1
[fragment] => anchor
)

--markc

Carl S. Yestrau Jr.

unread,
Aug 14, 2010, 12:50:51 AM8/14/10
to nod...@googlegroups.com
The fragment identifier functions a bit different than the rest of the
URI. To my understanding its processing is exclusively client-side
with no involvement with the server. That being said, I've seen it in
my nginx access logs.

Isaac Schlueter

unread,
Aug 14, 2010, 1:44:26 AM8/14/10
to nod...@googlegroups.com
On Fri, Aug 13, 2010 at 22:30, markc <mcons...@gmail.com> wrote:
> That probably is true in general but php is nice enough to provide the
> hash fragment...

Node is even nicer.

node> require("url").parse('http://u:p...@example.com/path?arg=1#anchor')
{ href: 'http://u:p...@example.com/path?arg=1#anchor'
, protocol: 'http:'
, slashes: true
, host: 'u:p...@example.com'
, auth: 'u:pw'
, hostname: 'example.com'
, hash: '#anchor'
, search: '?arg=1'
, query: 'arg=1'
, pathname: '/path'
}

The issue is that request.url doens't contain the hash, because the
browser typically doesn't send the hash, so the server doesn't have
it, and can't put it on request.url.


On Fri, Aug 13, 2010 at 21:50, Carl S. Yestrau Jr.
<ca...@featureblend.com> wrote:
> The fragment identifier functions a bit different than the rest of the
> URI. To my understanding its processing is exclusively client-side
> with no involvement with the server. That being said, I've seen it in
> my nginx access logs.

According to the spec, the client MUST send the path portion, but MAY
send the protocol, hostname, and hash. If there is a host header,
then the hostname in the request url MUST match the host header.

Browser behavior is convention only. If node gets the hash, and/or
the hostname, then it will provide it on the request.url.

--i

markc

unread,
Aug 14, 2010, 1:44:32 AM8/14/10
to nodejs
Apologies, only when provided as a string to parse_url() and not when
parsed to a server from a GET request. Perhaps use some client side
code that looks at windows.location.hash and sets a cookie.

Micheil Smith

unread,
Aug 14, 2010, 2:39:32 AM8/14/10
to nod...@googlegroups.com
You could also use client side JS to proxy the requests.. but I don't know why you'd
actually want to do that.

— Micheil Smith

Reply all
Reply to author
Forward
0 new messages