views:

326

answers:

2

Is there anyway to get a link to another part of your page (<a href="#link">) to not refresh the page? Some times it will not refresh the page, and other times it will. This is a problem as i also have that link doing some javascript changes to the page onclick, which are then discareded when the page is refreshed at the same time.

Any ideas?

code snippet:

<script type="text/javascript">
    function unhide(divID) {
     var item = document.getElementById(divID);
     if (item) {
      item.className=(item.className=='hidden')?'unhidden':'hidden';
     }
    }
        function unhidealways(divID){
               var item = document.getElementById(divID); 
               if(item){
                      item.className='unhidden';
               }        
        }
        function fillreply(commentID){
            var item = document.getElementById("replyto");
            item.value=commentID;
        }
        function hide(divID){
              var item = document.getElementById(divID); 
               if(item){
                      item.className='hidden';
               } 
              var item2 = document.getElementById("replyto");
              item2.value='';
        }
</script>

...

echo "<br /><span class=\"replytext\"><a href=\"#makecom\" onclick=\"javascript:unhidealways('makecomment');unhidealways('makereply');fillreply('" . $row['Id'] . "');\">[reply]</a></span><br />";

...

<td>
      <a name="makecom" />
      <a href="javascript:unhide('makecomment');"><i>Discuss</i></a>
      <div id="makecomment" class="hidden">
      <form name="commentform" action="comment.php" method="post">
        <div id="makereply" class="hidden">Reply to: <input type="text" size="6" name="replyto"/> <a href="javascript:hide('makereply');">[clear]</a></div>
        <input type="hidden" name="pageid" value="<?php echo $pageid; ?>" />
        <?php if(!$loggedin) 
             echo '<br /><a href="#TOP">Log In</a> or post as Anon<br /><br />';
             else
              echo '<br />';
        ?>
    <textarea name="comment" rows="7" cols="40" ></textarea>
    <input type="hidden" name="id" value="<?php echo $pageid; ?>" />
      <br />
    <input type="submit" value="Comment" />
    </form>
    </div>
</td></tr>
+4  A: 

It should never refresh if its a normal #link. Maybe you're catching the link with JavaScript which makes it go nuts? Maybe a return false; will help.

Also check that you are not adding a www. or taking it away or maybe just switching between http and https.

Thomaschaaf
Damn you @Thomaschaaf! 52 seconds faster!
voyager
only linking to "#link" no www. or http(s).
Petey B
A: 

I'd make sure that when you create the link that you're not including the rest of the URL, as you may be specifying a slightly different URL, seen as needing a new request to the server.

great_llama
only linkinh to "#this" nothing else.
Petey B