Reputation: 140
I would like to call a function each time a link is clicked. In this function, I will then call the appropriate page. Without creating a function for each link, how could I identify the clicked link? Here is a mixure of javascript and php to show how I want to do.
<div>
<ul><li><a id="address" href="clicked">address</a></li></ul>
<ul><li><a id="about" href="clicked">about</a></li></ul>
</div>
<?php
function clicked() {
$theaddress = $("#address").isClicked(); //looking at a function like isClicked
$theabout= $("#about").isClicked(); //do the same thing in case the function exists
if ($theaddress) {
//call the address page
}
else if ($theabout) {
// call the about page
}
}
?>
I would like to know if this is really possible. I know I could have a function for each link, but that looks like too many functions to me. So far this is what I got from stackoverflow
Upvotes: 2
Views: 125
Reputation: 943561
Forget JavaScript. If you just want one PHP program to handle two links and for each link to pass a bit of identifying data, then just put that data in the URL.
<ul>
<li><a href="clicked?id=address">address</a></li>
<li><a href="clicked?id=about">about</a></li>
</ul>
… and then:
$clicked = $_GET['clicked'];
Upvotes: 0
Reputation: 8457
You can use the onClick
mouse event.
inline:
<a href=# onClick="window.location.href = 'http://www.google.com'">HERE</a>
call a function (url inline):
<a href=# onClick="redirect('http://www.google.com')">HERE</a>
<script>
function redirect(key) {
window.location.href = key;
}
</script>
call a function (url inside):
<a href=# onClick="redirect('a1')">HERE</a>
<a href=# onClick="redirect('a2')">HERE</a>
<script>
function redirect(key) {
if (key == "a1") {
window.location.href = 'http://www.google.com';
} else if (key == "a2") {
window.location.href = 'http://images.google.com';
}
}
</script>
Upvotes: 1