How to open a alert box within anchor tag <a href>?
Asked Answered
T

5

10

I want to open a alert box when a certain link is clicked. How to do that ?

Thread answered 17/4, 2015 at 8:54 Comment(2)
What do you mean by "pop up"?Mosstrooper
possible duplicate of javascript popup alert on link clickWateriness
D
21

You can also use the following codes:

<a href="#" id="link">Link</a>

Javascript:

$(document).on("click","#link",function(){
 alert("I am a pop up ! ");
});

OR:

<a href="#" onclick="alert('I am a popup!');">Link</a>
Deplume answered 17/4, 2015 at 9:8 Comment(0)
R
5

Without using a JS file (purely as an alternative to answers already posted):

<a href="http://example.com" onclick="alert('Hello world!')">Link Text</a>
Radie answered 17/4, 2015 at 9:14 Comment(0)
T
4
<a href="#" onclick="myFunction(); return false;">

The return false; will stop the web page being loaded after clicking.

JavaScript code:

function myFunction() {
   alert("I am a pop up ! ");
}
Thread answered 17/4, 2015 at 8:54 Comment(0)
S
0

The following code might help:-

<a id="anchor">i am the anchor</a>
<div id="popup">this is a popup</div>
<script>
    $(function(){
        $('#popup').hide();
        $('#anchor').click(function(){
            $('#popup').show();
        })
    });
</script>
Shrubbery answered 17/4, 2015 at 9:27 Comment(0)
H
0

Simple JS payload with anchor tag when quotes are HTML encoded

<a href="javascript:alert(1)"> vish </a>

Hydracid answered 2/9 at 11:32 Comment(1)
What’s the functional or practical difference between this and @Shuma’s previous answer? Please edit to clarify.Kondon

© 2022 - 2024 — McMap. All rights reserved.