0

I've got a Master page, which loads my Default.aspx

I need to get the browser version using

function loadHiddenField() 
{       
    document.getElementById("HiddenField1").value =  document.documentMode;
}

The result of this needs to be passed to my Default.aspx page so that I can use it in a Linq query used to populate the page. This all happens when my site first loads.

So:
1) Type in my site URL
2) Page loads, displaying the correct information in the default.aspx page depending on what browser version you are using.

I originally tried to call the method using the client side body onload, but I realise that this won't work.. So I need to know what does work..

Please don't tell me to use HttpBrowserCapabilities, because that won't work.. What I have in the JavaScript above is what I need.

I'm stuck and really need help here.. I've asked this question previously, but the only person that tried to help deleted his post.

Matt
  • 4,140
  • 9
  • 40
  • 64
  • have you tried window.onload = function(){loadHiddenField();}? – scrappedcola Jul 18 '11 at 22:43
  • Where to I put that? And how would that get the value to the server side? Don't I need to force a postback? – Matt Jul 18 '11 at 22:48
  • My hunch is that the reason you're having a hard time getting an answer has to do with the fact that you're trying to do it with a nonstandard solution. Heaven knows we've all been in that situation, but I'm curious as to what you're specifically trying to do that the HttpBrowserCapabilities class won't do for you? – Michael Ames Jul 18 '11 at 22:50
  • Because on my intranet, all pc's are defaulted to use compatibility mode. So if I use HttpBrowserCapabilities, it always gets reported as IE7. The only way I've found to accurately get the browser version is to use document.documentMode, but I can't figure out how to pass the result to my Linq query. – Matt Jul 18 '11 at 22:56

3 Answers3

1
<script>
bDocumentNeedsReload = <%= Session('documentmode') >= 0 : 'TRUE','FALSE' %>

if( document.documentMode ) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "setsessiondocumentmode.aspx?mode=" + document.documentMode);
  xhr.send();

  if( bDocumentNeedsReload == TRUE ){
    window.location.reload();
  }
}
</script>

Take this with a grain of salt: I haven't tested it, but you can probably see where I'm going with this. Also, someone smarter than me will probably give you a compliant solution. Also, my C# is a bit out of date.

uotonyh
  • 786
  • 5
  • 7
  • The "setsessiondocumentmode.asp" will have one function: to set a server-side session variable that your Linq query will be able to read. Once that session variable is set, you can then have your default page write that bDocumentNeedsReload is false, and you can continue on your way. Make sense? – uotonyh Jul 18 '11 at 23:07
  • I think so yes.. I'll test it and report back. Cheers – Matt Jul 19 '11 at 00:04
  • Where do I declare bDocumentNeedsReload? – Matt Jul 19 '11 at 00:15
  • If someone can help me figure out the bDocumentNeedsReload bit I'd be grateful – Matt Jul 19 '11 at 04:46
  • Hi Matt, take a look at the edits above, and see if this makes more sense. Cheers! – uotonyh Jul 19 '11 at 21:33
0

You can use two different pages to solve this problem. The first page loads and the moment it loads it redirects to default.aspx passing the document.documentMode in the querystring. This way you have the browser version in the code behind.

function redirect()
{
    document.location = 'default.aspx?mode=' + document.documentMode; 
} 

Call redirect() in header of the page.

And in the page_load of default.aspx code behind fine you just need to use Request.QueryString["mode"];

gaurawerma
  • 1,826
  • 13
  • 16
0

You can set a variable in the request header via Javascript (see this SO questions). Here is an example:

<script type="text/javascript">
    function onLoad() {
        var request = new XMLHttpRequest();
        request.open("POST", "/Home/About", true);
        request.setRequestHeader("documentMode", document.documentMode);
        alert(document.documentMode);
        request.send();
    }
</script>

Note that you need to call Open() first. Also not all browser DOMs will have document.documentMode set! This example posts back to /Home/About, but you can address any endpoint on your server you'd like, maybe even persist this information into a cookie so subsequent requests will already contain this information.

The server code can then inspect the request header (the example below is for ASP.NET MVC):

string documentMode = Request.Headers["documentModel"];

Using the basic ASP.NET MVC 3 web site, the documentMode variable is set to '7' in my About() controller function.

Community
  • 1
  • 1
Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66