简体   繁体   中英

Remove # from href calling JS function?

Hey I have a function which is called from a html link like this:

 <a href='#' onclick='javascript:populate("m")'>My Messages</a><br/>

Function called:

 function populate(q){

    switch(q){
    case 'm':  messages_document(call_data(q+'.php','main')); break;
    }
    return (false);
  }

The problem i have is the url in the browser adds a # to the it - which seems to prevent me clicking the link again unless i remove the hash, is there a way to stop it loading in the browser url ?

onclick='return populate("m");'

Load your script in the document head(that is, stuff it between two <head> and <script type="text/javascript"> tags). Example :

<head>
    <script type="text/javascript">
        function test(id){
            alert(id);
        }
    </script>
</head>
<a href='#' onclick="test('Test');">My Messages</a>​

I've tested it and it works fine.

try

<a href="javascript:;" onclick="populate('m');">My Messages</a>
<script>
function populate(q) {
    switch(q) {
        case 'm':  messages_document(call_data(q + '.php', 'main')); break;
    }
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM