Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline javascript in href

How can you do something like this:

<a href="some javascript statement that isn't a function call;" >myLink</a>

And have the js in the href execute when the link is clicked.

like image 616
PhilBrown Avatar asked Apr 01 '11 21:04

PhilBrown


2 Answers

<a href="javascript:var hi = 3;" >myLink</a>

Now you can use hi anywhere to get 3.

like image 136
pimvdb Avatar answered Oct 27 '22 00:10

pimvdb


Just put the JS code directly in there:

<a href="#" onclick="a=1;b=2; return false;">fsljk</a>

Though, you should not be doing inline scripting. You should unobtrusively attach event handlers.

<a id="lol" href="/blah">fdsj</a>
<script>
document.getElementById('lol').onclick=function() {
/* code */
};
</script>
like image 36
meder omuraliev Avatar answered Oct 27 '22 00:10

meder omuraliev