Matthias
Matthias

Reputation: 1436

Avoid that jquery ajax removes #

The jquery ajax function removes anchors in URLs. Something.html#anchor

What if we have the # as a parameter for a script?

Can I prevent jquery from removing the anchor?

Upvotes: 0

Views: 83

Answers (4)

doublesharp
doublesharp

Reputation: 27599

The hash tag is not technically part of the url as it indicates an anchor on the page and is not sent to the webserver. If you need to get it to pass to the server as part of the form data sent with an AJAX request, you can access it with location.hash

var urlWithHash = location.href + location.hash;

Upvotes: 0

Andrea Turri
Andrea Turri

Reputation: 6500

You can not keep the #. You can concatenate it:

data.url + "#" + data.anchor

...or something similar.

Upvotes: 0

Jon
Jon

Reputation: 437336

No, because the part after the # (which is called a fragment) is never sent to the web server; it's not part of the HTTP request. It can only be accessed from client-side scripting.

It doesn't matter if you use jQuery, AJAX, or simply paste the URL in the browser's navigation bar and hit ENTER; this behavior is the same in all cases.

Upvotes: 2

nagylzs
nagylzs

Reputation: 4180

You cannot, because anchor is not part of the request. The anchor is interpreted on the client side (by the browser). If you want to send an anchor, then you will have to save it into a data structure and it to the server that way.

Upvotes: 3

Related Questions