0

I'm customizing a website that was build by someone else using a WYSIWYG. I'm stuck with an href having javascript in it.

How can I, onClick of this anchor tag, capture the href, change it's value to #, preventDefault, and then actually run what was going to originally be ran? I know this is a bit odd, but it's currently causing IE errors and I believe it will fix it.

HTML:

<a class="cpMonthNavigation" href="javascript:CP_refreshCalendar(3,1,2012);">&lt;&lt;</a>

jQuery:

$(document).click({namespace: this}, function (e) {
    var t = e.data.namespace;

    if ($(e.target).is($(".cpMonthNavigation"))) {
        e.preventDefault();

        $(e.target).attr("href").replace(/javascript:/i, "");
        // This is where I need to manually run the function that was in the HREF
    }
}

Please keep in mind that I cannot edit the HTML. I'm stuck with it and it's terrible-ness.

I need to preventDefault so that the vendor code does not run but still run what's actually in the href. There are multiple things behind the scenes in the vendor code bound to the click of this anchor.

2
  • Why do you want to capture the click and do the same thing? Commented Feb 15, 2012 at 17:11
  • Because there is a ton of build in vendor javascript that is tracking every click of an anchor tag and firing functions on every anchor tag click. I need to stop the vendor javascript (that I can't edit) but still run the javascript that is in the href. Commented Feb 15, 2012 at 17:14

3 Answers 3

1

Try this piece of jQuery code:

$(".cpMonthNavigation").click(function (e) {
    e.preventDefault();
    var expression = $(this).attr("href"); 
    eval(expression);
    $(this).attr("href", "#");
};
Sign up to request clarification or add additional context in comments.

1 Comment

@JesseAtkinson misunderstood your question. Updated the answer.
1

I may be making this simpler than needed but to simply run the function, preventing defaults:

$(document).on('click', 'a.cpMonthNavigation', function(e) {
    e.preventDefault();
    var myHref = $(this).attr("href");
    myHref = myHref.replace(/javascript:/i, "");
    var myNewFunc = myHref.replace(/javascript:/i, /""/i);
    eval(myNewFunc);
});

a working example here: http://jsfiddle.net/MarkSchultheiss/Vta56/

EDIT: shorter version of above:

$(document).on('click', 'a.cpMonthNavigation', function(e) {
    e.preventDefault();
    eval($(this).attr("href").replace(/javascript:/i, ""));
});

2 Comments

IF you have other links, just add the class in the 'a.cpMonthNavigation, a.newclass' part
note: my above code leaves the original href so that it does not break the function on other clicks - easily added if you want that part to REALLY be href='#' (left the above a bit wordy to show each step explicitly)
0

You can use eval to invoke the function. But that is considered as a bad practice in Javascript Programming.

The other way i could think of is, define the function inside an object like,

var operations = {
    CP_refreshCalendar : function(mm, dd, yyyy){
                              //your code
    }
}

Then, from the click event handler, you could do

$(document).click({namespace: this}, function (e) {
var t = e.data.namespace;

if ($(e.target).is($(".cpMonthNavigation"))) {
    e.preventDefault();

    var hrefVal = $(e.target).attr("href").replace(/javascript:/i, "");
    var funcName = hrefVal.substring(0, hrefVal.indexOf('(') - 1);
    var args = hrefVal.substring(hrefVal.indexOf('('), hrefVal.index(')') - 1);
    args = args.split(',');
    if(funcName in operations){
         operations[funcName].apply(window, args);
    }

}
}

May be i am making this complex. But i will not use eval.

I Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.