views:

84

answers:

1

I am trying to change the text in browser's address box by simulating a click on an anchor tag via javascript. This code works just fine in IE, but not in FireFox or Chrome.

<script type="text/javascript">
    function UpdateQueryString() {
        var controlRef = document.createElement('a');
        controlRef.id = "t1";
        controlRef.href = '#1';
        controlRef.innerHTML = '';
        document.body.appendChild(controlRef);

        try {

        controlRef.click();
            }
        catch (err) {
            txt = "There was an error on this page.\n\n";
            txt += "Error description: " + err.description + "\n\n";
            txt += "Click OK to continue.\n\n";
            alert(txt);
        }
        return false;
    }
</script>

In browsers other than IE, I can find the anchor tag, but calling the click results in an error.

Thanks

+5  A: 

You're looking for the location property:

location = '#1';

or

location.hash = '#1';
SLaks