Forum Moderators: open

Message Too Old, No Replies

Links to named values hit the server?

         

Fotiman

11:29 pm on Nov 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I have a link in my document defined like so:

<a href="#" onclick="return true;">Foo</a>

Should this result in another trip to the server? I always thought that because the href value only contained the # and not another URL, that it owuld be smart enough to know that it should search the current page for the link. However, I have some pages that pass in a value via the querystring and that value is used to retreive some data. When I click on a link like this, the data goes away because it hits the server again without the querystring value. Is this normal?

I know that putting "return false;" in the onclick event should prevent the server hit, but I didn't think it was necessary here. :-/

Fotiman

11:49 pm on Nov 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To elaborate, suppose I visit my page at:

index.htm?id=1

Then I click on a link in the page like this:

<a href="#" onclick="return true">Do some javascript</a>

Now my browser gets:

index.htm#

And the data that was being displayed as a result of the id=1 is now gone. Is this normal?

Thanks.

Dijkgraaf

2:29 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What browser are you using. I just tried it in IE 6 and FF 1.0.07 and in both cases it just added the #whatever on after the query string.
So no, it does not sound normal to me.

encyclo

2:32 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have a
<base href="http://example.com/">
on your page? Are you using Opera as your browser? (If so, check out this thread [webmasterworld.com] from earlier today.)

Dijkgraaf

2:40 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



good question encyclo that does seem to change the behaviour

Fotiman

3:05 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I do have a base tag. However, I don't get how that ties into that other thread. Also, in this particular case, I was using Firefox on a Linux box, but I think I may have seen it with IE6/Win as well.

How does the <base> element affect it?

Thanks.

Fotiman

3:23 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think I get it now.

So if I have a base tag like this:

<base href="https://localhost/foo/bar.htm">

But the location in my address bar looks like this:

[localhost...]

Then when I click on one of the links that has "#" as the href value, that equates to the URL in the base instead of the one with the query string.

Thanks, this has really helped!

Fotiman

4:34 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Will it still hit the server if I create the <base> tag with the query string values included? For example:

<base href="http://localhost/foo.htm?id=2">

pixel_juice

6:58 pm on Nov 15, 2005 (gmt 0)

10+ Year Member



From w3.org on base href tags:

This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.

[w3.org...]

As "#" is a relative URL, the base href will override any query string you may have.

If you can use server side scripting on your page, you could put the REQUEST_URI environment variable in front of your name link to resolve the problem (worked for me!)

Fotiman

8:22 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For clarification:


As "#" is a relative URL, the base href will override any query string you may have.

Note, "#" is a fragment, not a querystring. What I want is for my base URL to contain the entire path, including query string. Including the query string does not make it a relative URL.

As defined in RFC 1808 Relative Uniform Resource Locators [ietf.org]:

absoluteURL = generic-RL ¦ ( scheme ":" *( uchar ¦ reserved ) )

generic-RL = scheme ":" relativeURL

relativeURL = net_path ¦ abs_path ¦ rel_path

rel_path = [ path ] [ ";" params ] [ "?" query ]

Thus, absoluteURL could be:
scheme ":" path "?" query
or
https://localhost/foo/bar.htm?id=1

This is also reinforced in RFC 2396 URI Generic Syntax [ietf.org], section 3. URI Syntactic Components.

I just wanted to clear up any confusion. It sounds like I should be able to specify a base that includes the querystring, so I think that may work for me.

pixel_juice

10:10 pm on Nov 15, 2005 (gmt 0)

10+ Year Member



Note, "#" is a fragment, not a querystring.

Indeed - I wasn't suggestion it was a query string! I was just describing why the base href caused the problems in the first place. # relative to [localhost...] [localhost...]

<base href="http://localhost/foo.htm?id=2">

As you suggest, this will solve your problem since it will link to http*//localhost/foo.htm?id=2#. I suggested using REQUEST_URI as an alternative, since you could add this into any number of links without the need for having different base href tags.

Fotiman

11:07 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks pixel_juice.