views:

357

answers:

6

Hi,

I'm trying to create a JavaScript hyperlink which links to a HTML anchor. The only thing is, I'm trying to create it in SharePoint within an .aspx page...

I have a hidden table, which I unhide with a JavaScript function, but the table is at the bottom of the page, so I added an anchor next to the table and tried to hyperlink to it... but it's not working... This is my code:

function GoTo() {
    window.location.hash="change"
}

<a name="change"></a>

Any ideas?

A: 

Change your window.location.hash = "change" to window.location.href = "#change" I've tested this and this is working in IE8, Chrome, FF, Safari.

There is probably another way:

function scrollToAnchor(anchorName){
  //set the hash so people can bookmark
  window.location.hash = anchorName;
  //scroll the anchor into view
  document.getElementsByName(anchorName)[0].scrollIntoView(true);
}

If the hash method is not working try to set your window in a small size that it can jump to the hash tag

Robert Cabri
Wrong. Although the `#` character is required in the URL, it is not part of the `hash` property.
SLaks
Just to post again, as you posted after I did:That's not working, Gregoire. The code is now:function GoTo() {window.location.hash="#change"}But it's still not working... no JavaScript errors, by the way. Any ideas?
JD-Daz
Is it something that SharePoint might be blocking by any chance??
JD-Daz
A: 

I just tried it, and it worked for me. Try adding #change to the address bar and see whether it does what you expect it to.

SLaks
A: 

That's not working, Gregoire. The code is now:

function GoTo() {window.location.hash="#change"}

But it's still not working... no JavaScript errors, by the way. Any ideas?

JD-Daz
This should be a comment, not an answer. You're correct; he's wrong.
SLaks
A: 

JD-Daz -

Based on your brief spec, I created this simple test page, and it seems to work. Maybe you should do a copy and paste of your own page into a new page, and then cut stuff out until it works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script language="javascript">
        function GoTo() {
            var myTable = document.getElementById("myTable");
            myTable.style.visibility = "visible";
            window.location.hash = "change";
        }

    </script>
</head>
<body>

<a href="javascript:GoTo();">Click Here</a><br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
12<br />
13<br />
14<br />
15<br />
16<br />
17<br />
18<br />
19<br />
20<br />
21<br />
22<br />
23<br />
24<br />
25<br />
26<br />
27<br />
28<br />
29<br />
30<br />
<table id="myTable" border="1" width="500" style="visibility:hidden" backcolor="black">
    <tr>
        <td>Shown</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
</table>
<a name="change"></a>
</body>
</html>
Mark Bertenshaw
That's pretty much exactly how I had it. I created a test page and it worked perfectly fine. As soon as I placed it into SharePoint, it stopped working. I changing an ASPX page and inserting custom JavaScript, CSS and HTML - I've had no problem with it before, until this... and I have no idea why. Thank's for the effor though Mark, much appreciated
JD-Daz
A: 

To solve your problem, change to button to

<input type="button" value="Request For Change" onclick="GoTo(); return false;">

The problem is that an input type="button" will submit the page when clicked. Therefore, when you click it, it scrolls down to your anchor element, but then reloads the page, defeating the purpose. You need to add return false to the end of the onclick handler to suppress the default action.

Also, onclick should be all lower case.

SLaks
This hasn't worked. Thank's for the effort though, SLaks
JD-Daz
A: 

I think you want to use <a href="#change" onclick="unhide table code here">link text</a>.

Eli Grey