Forum Moderators: open

Message Too Old, No Replies

Loadlistener instead of inline javascript?

         

iceomnia

10:26 am on Oct 29, 2009 (gmt 0)

10+ Year Member



Hi Guys,

I might be completely barking up the wrong tree but instead of having a link that looks like this:


<a href="http://www.example.com" id="1234" onmousedown="javascript:urlout('/out.jsp?url=http://www.example.com');">Click here</a>

Is there some way of having the url look like this:


<a href="http://www.example.com" id="1234");">Click here</a>

The id is present in the link so is this possible to catch the onmousedown event without it being in the link it's self?

Fotiman

2:51 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are not barking up the wrong tree. :)

Using Unobtrusive JavaScript is a good thing. You want to keep your markup clean and your JavaScript separate.

You can use your own methods for attaching event listeners. Here is one way using the Yahoo UI Library:
(Note, your id attribute is invalid. Id's must start with a letter, not a number, so in my example I've replaced "1234" with 'YOURID')


<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js" ></script>
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function () {
// Attach your listener
YAHOO.util.Event.on('YOURID', 'mousedown', function (e) {
urlout('/out.jsp?url=' + this.href);
});
});
</script>

Hope that helps.