Click to See Complete Forum and Search --> : getting the anchor name.


gwildor
03-13-2003, 03:08 PM
helllp.. hehehe.. i know you guys are amazing.. :D

i'm trying to get my javascript to get the anchor name that the page is trying to access.. but i'm not sure if i'm doing this right or if it's possible.. this is for furthur script development.

i have this soo far.



<HTML>
<HEAD>
</HEAD>

<BODY>
testing
<br>

<SCRIPT LANGUAGE="javascript">
<!--

var anchorname=document.anchor

document.write.anchorname


// -->
</script>

</BODY>

</HTML>



this may look like simple code.. but basically i want to use the anchor name as a variable for furthur on in the script..
so that when i make a call like page.html#anchor that i can use that anchor as a variable.. does that make sense??


help :)

Jona
03-13-2003, 05:25 PM
First I don't think that you've got your document.write written correctly. It should be document.write(anchorname). Second, that script probably wouldn't ever work, because document.anchor keeps returning undefined. Why don't you try parsing the URL?

Jona
03-13-2003, 05:45 PM
Stupid, stupid, stupid! I hate that... I forgot about ".length"! Ugh...:mad:

I hope I feel better tomorrow...

gwildor
03-14-2003, 09:25 AM
im not sure i understand.. so would something like this work??

<SCRIPT LANGUAGE="JavaScript">
<!-- //
var ary = document.anchors;
var i, str = "", len = ary.length;
for (i=0; i<len; i++) {
str += ary[i].name + "\n";
}
alert(str);

if str = ("webdeveloper")
{
window.location = "http://www.webdeveloper.com"
}
// -->
</script>

gwildor
03-14-2003, 10:22 AM
<a name="webdeveloper" id="webdeveloper">



<SCRIPT LANGUAGE="JavaScript">
<!-- //
var ary = document.anchors;
var i, str = "", len = ary.length;
for (i=0; i<len; i++) {
str += ary[i].name + "\n";
}
alert(str);

if (str = "gwildor")
{
window.location = "http://www.gwildor.com"
}
else if (str = "webdeveloper")
{
window.location = "http://www.webdeveloper.com"
}

// -->
</script>

gwildor
03-14-2003, 04:17 PM
i scrapped the above and am trying this..


this is what i used for a link..
http://www.page.com/page.html#webdeveloper

and this is what i have in page.html.. and i'm hoping page.html would now redirect the page to webdeveloper.com
<SCRIPT LANGUAGE="JavaScript">
<!-- //

if (document.anchors=="webdeveloper"); { window.location = "htp://www.webdeveloper.com" }
elseif (document.anchors=="gwildor"); { window.location = "htp://www.gwildor.com" }

// -->
</script>

Jona
03-14-2003, 05:37 PM
Put this in your body tag:

<a name="gwildor" onclick="ankors()" href="#">Go to....</a>
<SCRIPT>
function ankors(){
var ary = document.anchors;
var i, str = "", len = ary.length;
for (i=0; i<len; i++) {
str += ary[i].name;
}
alert(str)
if(str == "gwildor"){location = "http://www.gwildor.com" }
else if(str == "webdeveloper"){location="http://webdeveloper.com/"}
else { location = "http://www.msn.com/" } }
</script>

If you want to make it go to webdeveloper.com, just change <a name="gwildor"> to <a name="webdeveloper">. If you use anything else, it'll go to msn.com, but you can change that, too if you like.

I hope you noticed that you were using one = sign, instead of two. One is the assignment operator, two is the comparative operator.

khalidali63
03-14-2003, 07:04 PM
Originally posted by Dave Clark
If you actually have any anchors (bookmarks) in your page,............
Dave

I am just hoping that you followed the above,

document.anchors returns an array of anchors on a document that are "named", this means that any anchor tag that has a name attribute set for it will only be returned by the document.anchors others will not be,
now here is "yet" another working example.( this example presumes there are only 2 anchors with names on the page.


<script type="text/javascript">
function Process(){
var links = document.anchors;
var len = links.length;
for(x=0;x<len;x++){
if(links[x].name=="google"){
window.location.href = "www.google.com";
}else if(links[x].name=="netscape"){
window.location.href = "www.netscape.net";
}
}
}

</script>
</head>

<body class="body">
<a href="www.cnn.com">cnn</a><br/>
<a name="netscape" href="www.netscape.net">netscape</a><br/>
<a href="www.yahoo.com">yahoo</a><br/>
<a name="google" href="www.google.com">google</a><br/>
<form name="form1" action="" onsubmit="">
<input type="Button" value="process" onclick="Process()"/>
</form>


Cheers

Khalid

gwildor
03-18-2003, 02:50 PM
These examples are excellent and they've definately helped.. But is there a way that i can let's say take a document that contains no anchors and write an anchor and then use the examples below??

my example...

http://www.gwildor.com/redirect.html#google

how do i capture that "#google" and write it to an anchor.

gwildor
03-18-2003, 03:45 PM
nevermind guys!! I figured it out... :)

<script type="text/javascript">
<!-- //

if(location.hash=="#google"){
window.location = "http://www.google.com";
}else if(location.hash=="#gwildor"){
window.location = "http://www.gwildor.com";
}else {
window.location = "http://www.webdeveloper.com";
}

document.write(location.href)

-->
</script>