I'm trying to execute javascript after a link is clicked before loading the link, using this code:
$('body').on("click", 'a', function (evt) {
evt.preventDefault();
console.log(this);
$('.content').addClass('hide');
if (this.search("AV") > 0) {
$('#AVheader').addClass('fullwidth');
}
setTimeout(function () {
window.open(this, "_self");
}, 500);
});
My errors are:
this.search
Isn't a function and
window.open(this, "_self");
doesn't work
Wanted to put the question out there since I couldn't find a simple explanation and I'd probably spend half a day trying to figure it out..
When you are in the event handler
this
points to the clicked link. There is nosearch
method for an element. Probably you are trying to search for a string somewhere in the link, maybe the content.