Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding javascript to hyperlink

Tags:

javascript

How can i add a Javascript alert to the hyperlink ? I want to make the alert pop up when someone click on the link . This is the script for now -

<a href="<?php echo ($link1);?>" onclick="popitup();" data-role="button" data-theme="b" data-inline="true" > Send </a>

And i want to add this to the link

<script type="text/javascript">alert('Text.');</script>
like image 211
Ido Bukin Avatar asked Apr 19 '26 17:04

Ido Bukin


2 Answers

You should define the popitup() function that you are calling in your onclick attribute:

<script type="text/javascript">
    function popitup() {
        alert('Text.');
    }
</script>

also if you want to prevent the link from redirecting under certain conditions you could return false from this handler:

<a href="<?php echo ($link1);?>" onclick="return popitup();" data-role="button" data-theme="b" data-inline="true"> Send </a>

and then:

<script type="text/javascript">
    function popitup() {
        if (confirm('Are you sure you want to do that?')) {
            return true;
        }
        return false;
    }
</script>
like image 187
Darin Dimitrov Avatar answered Apr 22 '26 06:04

Darin Dimitrov


if the alert should do some kind of logic you can use the return function like this

<a href="newlink.html" onClick="return confirm('do you really wanna go to this link?')" >the link</a>

or just add the alert w/o any logic in this way

<a href="newlink.html" onClick="alert('you are going to the link! beware!')" >the link</a>
like image 31
Naraj Avatar answered Apr 22 '26 07:04

Naraj