What is the reason for putting ? in the href. Eg. href="?"

Hello,

On page 278 of “PHP and MySQL: Novice to Ninja”


...
<a href="?">Continue shopping</a>
...

What does the ? do in the href attribute?

Thanks

Leaving href empty would link to the same page the link is located on. So if on index.php, <a href=“”>test</a> will link to index.php. With “?” set as a href attribute, it’ll link to index.php? - having no (useful) effect at all, except maybe reloading page :slight_smile:

Just wait until HTML 5, when the href is entirely optional :wink:

Avram is correct in that it links you to the page you are currently on. However there IS a difference between “” and “?”. Linking with “” will reload the current URL with the same GET variables attached. Linking with “?” will remove all GET (query-string) variables from the URL.

So if you’re on index.php?members=me, and click a link with href=“”, you’ll go to “index.php?members=me”. If you click on a link with href=“?”, you’ll go to “index.php?”

1 Like

Isn’t it already optional? If you’re defining an anchor, rather than a link? ie <a name=“news”>News</a>

I presume you’re saying that you would be able to do simply <a>News</a> or <a id=“news”>News</a> and do something with JS instead?

Unless I am misreading the HTML 5 document i’m looking at, any <a> that has content without an href attribute would be treated as if it had href=“”.

If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element’s contents.
Right near the top. Haven’t read the whole thing, but using <a name=“foo”> is the traditional way to link to a part of the page. I believe it may be deprecated in favour of linking directly to an id on a non-anchor element

Correction. <a name=“foo” /> is the traditional way. A content-less tag.

A) I didn’t mean content-less, I just didn’t enter the content or closing tag, so it would be <a name=“foo”>Foo</a>
B) Yours is XHTML, which IMHO should never be used unless you need to parse the content with an XML parser anyway :slight_smile:

avram and starlion,

those explanations make sense in light of the examples i was looking at.

It is perfectly reasonable to use XHTML as long as you don’t need to support IE8. That’s why all that XHTML5 development work with all the new tags is currently underway.

Once IE8 is dead HTML will become unnecessary as all browsers will then support the far superior XHTML5 variant.

wait what? IE 8 doesn’t use xhtml? so what should I use? just regular html in the meantime? how prevalent is ie 8?

Sorry @NewWebDesigner2, didn’t see your reply until months later.

XHTML is considered malformed HTML by most browsers unless you also use the correct MIME type, ie application/xhtml+xml. But if you used that in early IE versions you would see an XML DOM, and not a rendered web page. So if you were sending XHTML code, the browser was treating it as HTML anyway. There’s no harm in using XHTML, and it’s important to use it if you need to process it using an XML parser of some kind (don’t forget that XML is strict, compared to HTML), but it wouldn’t validate correctly.

See here for more information: http://www.w3.org/TR/xhtml-media-types/#media-types

You’ll see that there are acceptable usages of XHTML and text/html (the default for most webservers) but I suspect that they won’t cover most of your use cases.

Lots of people think they’ve been using XHTML for years, but they haven’t been doing it properly, frankly.