Click to See Complete Forum and Search --> : Passing variables to an anchor point


nluoma
02-26-2003, 11:10 AM
I'm trying to pass a variable to a page. My code is as follows:

To link to a new page:
<a href="../gloss.htm" onClick="intercept(this)">Text</a>

Which calls the function:
function intercept(what) {
what.href += '?db=' + db; //adds the correct db scenario (?db=...) to the end of the URL
}

Essentially what happens is, the link becomes
../gloss.htm?db=abi

However, when I do this with a link to an anchor, it doesn't work.
I've put in alert boxes to show me the values, and they claim that the path is correct:
../gloss.htm#anchor?db=abi
but when it loads into the page it turns to
../gloss.htm?db=abi#anchor
and of course it doesn't go to the correct location on the page.

Is this a limitation of the browsers? Is there a way around it?

gil davis
02-26-2003, 11:43 AM
This may be browsers specific. The href should contain the entire URL for the link, but there is another part called "hash" that has the part after the "#". You could try:

what.href += what.hash + "?db=" + db;

An alert will help you debug.

Charles
02-26-2003, 12:03 PM
Give this one a go:

<script type="text/javascript">
<!--
var db = 'foo';
// -->
</script>
<a href="../gloss.htm#anchor" onclick="this.href = this.href.replace (/#/, '?db=' + db + '#')">Text</a>

nluoma
02-26-2003, 12:32 PM
The problem doesn't seem to be the code, the problem seems to be that you can't combine both a ? and a # in a URL. Just discovered that you can't even manually type this in. If I type the address correctly into the browser bar, it changes the addresses, moving the # to the end of the URL...
I guess it just isn't supported.

Charles
02-26-2003, 12:41 PM
Yes, in a valid URL the fragment identifier always goes at the end and you will notice that my example keeps it there. Give it a try.