2

I have a link like that. It's getting from instagram api.

http://localhost:60785/access_token.aspx/#access_token=43667613.4a1ee8c.791949d8f78b472d8136fcdaa706875b

How can I get this link from codebehind? I can take it with js but i can't get it after assign to label. I mean:

<script>
    function getURL(){
        document.getElementById('lblAccessToken').innerText = location.href;
    }
</script>

This js function is in body onload event. How can I reach this innerText value from codebehind?

Kadir
  • 3,094
  • 4
  • 37
  • 57
  • 3
    The `#...` part is never sent to server. – Felix Kling Apr 23 '12 at 00:27
  • I know that, but we can get it with javascript and assign to label. But I can't get the label value from codebehind. – Kadir Apr 23 '12 at 00:29
  • Ah, then your question title confused me... `innerText` is not available in Firefox, maybe that's the problem. Or an element with ID `lblAccessToken` does not exist. Or this element does not accept any content. Without more information, it is impossible to answer. – Felix Kling Apr 23 '12 at 00:30
  • jQuery is an option? i mean asp.net could also mangle your id and so its better get element by class. – naveen Apr 23 '12 at 00:33
  • @naveen, how can I use it? Any example? This is result after html render. – Kadir Apr 23 '12 at 00:36

1 Answers1

1

If you are using ASP.NET 4.0 and jQuery, its fairly easy. Otherwise you may have to deal with mangled id and have to deal with DOMReady on your own. Try this

Markup

<asp:Label ID="lblAccessToken" runat="server" ClientIDMode="Static"></asp:Label>

JavaScript

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var myToken = GetHashParameterByName("access_token");
        $("#lblAccessToken").html( myToken );
    });

    function GetHashParameterByName(name) {    
        var match = RegExp('[#&]' + name + '=([^&]*)')
                .exec(window.location.hash);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));    
    }
</script>

You want the value on Page_Load right? I haven't figured out a way myself to fetch the hash value on Page_Load.I usually do one of these things

  1. Pass the hash value to a jQuery ajax method and store it there.
  2. Grab the hash value and redirect to the same page after converting it to a querystring

JavaScript

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var myToken = GetHashParameterByName("access_token") || "";
        if(my_token !=== ""){
            window.location = window.location.split("/#")[0] + "?access_token=" + myToken;
        }
    });

    function GetHashParameterByName(name) {    
        var match = RegExp('[#&]' + name + '=([^&]*)')
                .exec(window.location.hash);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));    
    }
</script>

Now at Page_Load, grab it like

string token = Request.QueryString["access_token"];

Please note that it takes one more round trip to the server and so not very efficient. But this is what I do.

naveen
  • 53,448
  • 46
  • 161
  • 251
  • To do the above without jQuery, `replace $(document).ready(…)` with `window.onload = function(){…}` and `$("#lblAccessToken").html( myToken ); ` with `document.getElementById('lblAccessToken').innerHTML = myToken;`. – RobG Apr 23 '12 at 00:50
  • 1
    :) i know. I thought DOMReady was safer. thats all. i was more concerned about not using DOMReady. And I decided not to throw this at OP. http://stackoverflow.com/a/1207005/17447 – naveen Apr 23 '12 at 00:53
  • Fine, I'd just rather see POJS and let the OP adapt it to their library of choice. – RobG Apr 23 '12 at 03:56
  • it's ok right now, i can get it. But i could not get the value of label from codebehind. – Kadir Apr 23 '12 at 21:28
  • Actually I still could not take the value from codebehind in page_load event. It must be postback to get value. So I fix it to redirect the page like window.location = 'blabla.aspx?token=' + myToken; – Kadir Apr 26 '12 at 11:59