Forum Moderators: open

Message Too Old, No Replies

Getting JS to add and read URL anchors

         

ahmedtheking

8:11 pm on Jun 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way I can get JS to add a URL anchor and (subsequently) when a page is loaded, read an attached anchor? Eg:

onclick: function () >> adds '#thisvar' to end of url (to form: webmasterworld.com/somepage#thisvar)

lavazza

7:28 am on Jun 18, 2007 (gmt 0)

10+ Year Member



When you say 'subsequently', do you mean AFTER the next page has loaded?

If so, no... Or rather I'd be very surprised - because JS is a client-side scripting language... and, as far as I know, passing parameters from one page to another is something any decent browser is designed to prevent - for security reasons

If not, then yes...


<html>
<head>
<script type="text/javascript">
function setMyDefaultValues()
{
document.frmGoThere.txtUri.value = 'http://www.webmasterworld.com/';
document.frmGoThere.txtAnchor.value = 'javascript/3370381.htm';
}
function goToMyHref(theURI, theAnchor)
{
var myUri = theURI.value;
var myAnchor = theAnchor.value;
var myLocation = '';
myLocation = myUri + myAnchor;
window.location.href = myLocation;
}
</script>
<title>My Title Goes Here </title>
</head>
<body onload = "setMyDefaultValues();">
<h1>
Concatenating a URI and Anchor
</h1>
<p>
<form name="frmGoThere"
method="post"
action="">
<input disabled
class="myClass"
name="txtUri"
id="txtUri"
size="129"
maxlength="128"
type="text">
<br/>
<input class="myClass"
name="txtAnchor"
id="txtAnchor"
size="129"
maxlength="128"
type="text">
<br/>
<inputtype="button"
name="btnNextHref"
id="btnNextHref"
class="myClass"
value="Click Me and Go"
onClick="goToMyHref(txtUri, txtAnchor);">
</form>
</p>
</body>
</html>

ahmedtheking

7:30 pm on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah after the page has loaded. After some googling, I came across this: [maxkiesler.com...] (follow the link to the test)

You can see that this adds #var to the URL without reloading the page.