简体   繁体   中英

Getting hash parameters from request url

I have such url - http://www.coolsite.com/daily-plan/#id=1 What the easiest way to parse that string and read a hash value (the value after #id=)? Thank you

On client side (ie from JavaScript) you can check window.location.hash to get hash. On server side, general answer is 'it is impossible' since hash is not sent in request to server.

Upd: I maybe misunderstood the question. My answer is about how to get hash part of url either in browser or in server side code during request processing, not about string processing.

Upd2: Answer to comment here because it doesn't fit in comment.

How does it work when user clicks on your navigational links?

I assume hash is changed and corresponding content is downloaded via AJAX request from web service or REST.

For example if your user has URL www.example.com in his browser and this page shows a list of product categories. User clicks one category and URL changes to www.example.com/#id=5 and products from that category(with ID=5) are downloaded via AJAX and shown on the page. No postback, only partial page refresh.

Is this close to your scenario?

Now you want user to paste/enter www.example.com/#id=5 directly in the browser address bar and go directly to list of products in that category.

But /#id=5 is not sent to server with request by the browser, so there is no way to get that value on server side, and you can do nothing about it since it is the browser decided not to send this data and you don't have it on server side.

In our project we use solution when server returns only common page code/html, ie header, footer, without main/center part of the page. Then there is a JavaScript code which executes right after this common HTML loaded. It takes window.location.hash and sends it to web service via AJAX and web service returns content (HTML) for the main part of the page.

new URI("http://.../abc#xyz").getFragment();

请参阅URIJavadocs

Here is how to capture anchor links. Works across all web frameworks.

I'll use an example scenario to illustrate: let's say we need to capture a deep URL http://server.com /#/xyz requested by an unauthenticated user so that they can be redirected to that deep URL post-login.

  1. The unauthenticated user requests http://server.com /#/xyz (everything from the '#' onwards is not sent to the server).

  2. All the server knows is that the user wants http://server.com/ and that they are unauthenticated. Server redirects the user to a login form.

  3. Here's the clever bit: the client is still waiting on their original request so if the server includes a hidden element in the login form with some JS that references window.location.href, it can capture the full URL of the original request complete with the anchor portion:

     <form action="/login" method="post"> <div> <label>Username:</label> <input type="text" name="username"/><br/> </div> <div> <label>Password:</label> <input type="password" name="password"/> </div> <!-- XXXXXXXXX CLEVER BIT XXXXXXXXXX--> <script> document.write('<input type="hidden" name="from" value="'+document.location.href+'"/>'); </script> <!-- XXXXXXXXXX--> <div> <input class="submit-button" type="submit" value="Submit"/> </div> </form>
  4. The user authenticates themself and the original URL is sent with the POST. The server can then relay the user to the original deep URL.

REPLACE the '#' with '?' when parsing the url. Check the code below

String url = "http://www.coolsite.com/daily-plan/#id=1";
String urlNew = url.replace("#", "?");
String id = Uri.parse(urlNew).getQueryParameter("id");
String url = " http://www.coolsite.com/daily-plan/#id=1";
int sharpPos = url.indexOf('#');
String q = null;
if (sharpPos >= 0) {
    q = url.substring(sharpPos);
}

Surely you can use various methods of string manipulation including regular expressions.

But actually your example is strange. Typically parameters of URL are passed after question mark. In this case you can just use standard class URL:

String q = new URL(" http://www.coolsite.com/daily-plan?id=1").getQuery();

what you are using to do this ?

If you are using jsp or servlet following will be useful to you

if (request.getParameter("#id") == null) {
    out.println("Please enter your name.");
} else {
    out.println("Hello <b>"+request.getParameter(i)+"</b>!");
}

If you are using javascript for it following function will be useful to you

function getURLParameters() 
{
var sURL = window.document.URL.toString();

if (sURL.indexOf("?") > 0)
{
    var arrParams = sURL.split("?");

    var arrURLParams = arrParams[1].split("&");

    var arrParamNames = new Array(arrURLParams.length);
    var arrParamValues = new Array(arrURLParams.length);

    var i = 0;
    for (i=0;i<arrURLParams.length;i++)
    {
        var sParam =  arrURLParams[i].split("=");
        arrParamNames[i] = sParam[0];
        if (sParam[1] != "")
            arrParamValues[i] = unescape(sParam[1]);
        else
            arrParamValues[i] = "No Value";
    }

    for (i=0;i<arrURLParams.length;i++)
    {
        alert(arrParamNames[i]+" = "+ arrParamValues[i]);
    }
}
else
{
    alert("No parameters.");
}
   }

if your url get from OAuth callback,then you can't! because the full url won't send to service because of hash(#)

If you URL will the same as you write and doesn't contains anythins else then whis code on Java will help you

 String val = "http://www.coolsite.com/daily-plan/#id=1";
 System.out.println(val.split("#id")[1]);

Don't forget check to null value.

PS If you use servlet you can get this parameter from request.getAttribute("id").

With best regards, Psycho

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM