Для этого вам нужно прикрепить обработчик к определенной привязке на странице.Для таких операций гораздо проще использовать стандартный фреймворк, такой как jQuery .Например, если бы у меня был следующий HTML
HTML:
<a id="theLink">Click Me</a>
, я мог бы использовать следующий jQuery для подключения события к этой конкретной ссылке.
// Use ready to ensure document is loaded before running javascript
$(document).ready(function() {
// The '#theLink' portion is a selector which matches a DOM element
// with the id 'theLink' and .click registers a call back for the
// element being clicked on
$('#theLink').click(function (event) {
// This stops the link from actually being followed which is the
// default action
event.preventDefault();
var answer confirm("Please click OK to continue");
if (!answer) {
window.location="http://www.continue.com"
}
});
});