Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I submit a hash key in an URL parameter?

I have a Spring-MVC application with Freemarker as the view component.

In my templates, several links are generated which point back to my application and which include URL parameters containing a hash key (#).

Example:

parameter: Q#106368 11

URL generated by Freemarker with encoded param: testurl.html?key=Q%23106368%2011

I use JavaScript to redirect to this URL (reason: I use JS to manage loading of 2 frames at the same time).

The redirect method is simple:

    function redir(url) {
        window.location.href = url;
    }

The JS call generated by Freemarker looks like

<a href="javascript:redir('http://localhost:8080/testappp/testurl.html?key=Q%23106368%2011');">test</a>

My problem is that the browser / Javascript converts back the URL encoded parameter, thinks there is a # and cuts off there.

When I use window.location.href='http://...' directly it works. Only when using the method parameter it seems to be magically URL decoded and then the redirect fails because the URL gets cut off at the #.

Is there an easy way to transmit the parameter correctly?

I am aware that I could replace the #, e.g. with $$$hash$$$, in the template and do the replacement on the server side again. But there are so many places I would have to change...

like image 955
tvirtualw Avatar asked Jul 08 '11 16:07

tvirtualw


People also ask

How do I use a hash mark in a click-through url?

There are several ways to use a hash mark or pound sign ( #) in a click-through URL. But first, some background. In a URL, a hash mark, number sign, or pound sign ( #) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier.

What does a hash sign mean in a URL?

A hash sign (#) in a URL is referred to as a fragment. Historically, URL fragments have been used to automatically set the browser’s scroll position to a predefined location in the web page. In that sense, if a URL refers to a document, then the fragment refers to a specific subsection of that document.

How to get hash parameters from request URL with JavaScript?

To get hash parameters from request URL with JavaScript, we can use the URL instance’s hash property. to create a new URL instance from a URL string. Then we get the hash value of the URL from the hash property. Therefore, hash is '#xyz'.

How to pass a parameter through the URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&". Read more about passing parameter through URL.


1 Answers

As Marc B commented, it is necessary to URL encode again. The method would be encodeURI(). However, this method does not encode the # sign. For my specific use case, I have to replace the # sign with %23 after the encoding.

The redirect JS method finally looks like:

    function redir(url) {
        url = encodeURI(url);
        url = url.replace(/#/g, '%23');
        window.location.href = url;
    }

Comparing escape(), encodeURI(), and encodeURIComponent()

like image 90
tvirtualw Avatar answered Sep 19 '22 00:09

tvirtualw