Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading URL Anchor on IE

I've got a page full of links to another page with anchors on the end (like this: index.html#anchor). On the page they point to, I have a script that is supposed to read where the anchor points to in order to display something.

On firefox it works perfectly, But I've noticed that IE seems to remove the #anchor from the end of the url, so the script can't grab the text. Is there a way around this, without any server side code?

like image 942
tominated Avatar asked Apr 29 '10 23:04

tominated


2 Answers

How is it getting the url?

window.location.hash should contain the contents of the hash.

like image 63
wombleton Avatar answered Nov 15 '22 20:11

wombleton


I've tested the following code in IE 6, 7, and 8, and the correct hash is shown in the alert box in all cases.

<script type="text/javascript">

    function showHash() {
        var currentUrl = "" + document.location;
        var hash = "";
        var parts = currentUrl.split("#");
        if (parts.length > 1) {
            hash = parts[1];
        }
        alert("the current hash is: " + hash);
    }

</script>

<input type="button" value="Show Hash" onclick="javascript: showHash();" />

Does that code work for you?

like image 28
David Mills Avatar answered Nov 15 '22 21:11

David Mills