Retrieving anchor value from URL

Lately i have been trying to get a grasp of the anchor value from the URL using ASP.NET. From what i learned from the forum (This might be wrong since there is a way in PHP(http://www.php.net/manual/en/function.parse-url.php) to get the anchor value.)

Case study:

e.g http://www.somesamplebigsite.com/product.aspx?q=123&u=09#description
The url above send the user to a page product and then your browser interpret the anchor and scroll down way to the description anchor. But the question is why the heck would i want to get the anchor since it’s just an anchor ? There might be several reasons for that. Here is 2 of them:

  • You might not want to pass an extra parameter in your URL query string because of Referencing issues
  • You would like to perform an action when the page reaches the anchor: Example open an AJAX popup or load some information or Execute a JavaScript in the Description Section of the product.

There might be several other reasons, but these two give you and idea of what is to be achieved.

You can’t get access to the anchor value using server side ASP.NET, it will neither be seen in the Request.Url nor the Request.RawUrl. Therefore those who are trying in vain to get access to this information, don’t bother anymore. You can just use a simple javascript that will do that, and then if you want afterward to either make a new submit to the server or do it in AJAX.

var anchorValue;
var url = document.location;
var strippedUrl = url.toString().split("#");
if(strippedUrl.length > 1)
anchorvalue = strippedUrl[1];

With these few lines of javascript you will be able to determine the value anchor that has been sent in your URL.

BUG: Error message when you visit a Web page or interact with a Web application in Internet Explorer: “Operation aborted”

Here is a bug that i came to recently while working with:

var d = document.createElement(“div”);
document.body.appendChild(d);

This bug is only on IE and it’s really annoying one. There is a fix provided by Microsoft, you can take a look at it here.

It is a pretty comprehensive and complete article but there is one thing missing in there. The state that the problem is caused

This problem occurs because a child container HTML element contains script code that tries to modify the parent container element of the child container. The script code tries to modify the parent container element by using either the innerHTML method or the appendChild method.

The solution is

To work around this problem, write script blocks that only modify closed containers or that only modify the script’s immediate container element. To do this, you can use a placeholder to close the target container, or you can move the script block into the container that you want to modify.

That’s a pretty good solution, but what if i don’t want to do that, or can’t handle a code that would generate something like that in the body, e.g When developing in ASP.NET using Master Pages. You must develop some specific module to have a handle in the master page body to be able to call your javascript in the body text itself.

Here is a quick fix that works pretty fine and easy to implement.

In your Page where you want to call the javascript create a DIV element in the body/content ( <div id=”ie_fix”></div> )
With this done you can now modify your javascript to be like that:

var d = document.createElement(“div”);

if(is_ie) //This is a browser check have to code here.
{
var ie_fix = document.getElementById(“ie_fix”);
ie_fix.appendChild(d)
}else
document.body.appendChild(d);

This is a quick fix that should help you waste/avoid unnecessary recoding. This has been Tested on IE 7 – Running on Windows Vista.