« Return to Thread: Change href of external links

Re: Change href of external links

by agent2026 :: Rate this Message:

Reply to Author | View in Thread

Having a hard time with this Dave, and I'm afraid the thread has moved a little ahead of my level.  Here's where I'm at:

var intLink = ['site1.com/','site2.com/'];

function linkExc(){
if (document.getElementsByTagName) {
        var a = document.getElementsByTagName("a");
                for(var h=0; h<intLink.length; h++){
                 console.log(intLink[h]);
                 for(var i=0; i<a.length; i++){
                         if(a[i].href.match(intLink[h])){
                                 console.log(a[i]);
                         }
                         else {
                                 a[i].onClick = window.open(this.href.value);
                         }
                 };
                };
}
};

I've resorted to straight js for the meantime as I was having trouble with jQuery.  One problem is I don't seem to be able to set the onClick event, though I am able to set other attributes when testing (title, etc.).  

Regardless, I would like to use .bind here, but I'm not sure how to turn this all into jQuery, or at least making the combination of the two you suggested, as there is no equivalent to .match that I know of.  The only solution I see is to put all the matching href values into a new array (which will hold the FULL urls - args, session ids, etc.), then using .grep to go through the array and .bind the click event, but that seems excessive.  Instinct tells me there must be a better way :).

Any help appreciated,
Adam


dave.methvin wrote:
> Am I mistaken, or does the string have to match
> exactly in this case?

Yep, but you could implement a left-case-insensitive match or even a
regexp-based pattern match. The jQuery part wouldn't change at all.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

 « Return to Thread: Change href of external links

LightInTheBox - Buy quality products at wholesale price