Change href of external links

View: New views
8 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Re: Change href of external links

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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@...
http://jquery.com/discuss/

Re: Change href of external links

by Jonathan Sharp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 12/14/06, Yehuda Katz <wycats@...> wrote:
> If that was the case, jQuery("#main a").filter(function() { return false; }) would return nothing. But it doesn't. It returns the original group of elements. At least for me.

Hm... I also tested this and filter didn't prune the list with my function returning false.

Here's the filter function out of 1.0.3 which checks if t is a function and pushes the result of jQuery.grep(this, t) onto the stack.

filter: function(t) {
        return this.pushStack(
            *SNIP*

            typeof t == "function" &&
            jQuery.grep( this, t ) ||

            jQuery.filter(t,this).r, arguments );
    },

Now in the grep function it operates on the list of elements in elms. It calls the function we defined (fn) and if fn returns false it shouldn't add them to the result array.

grep: function(elems, fn, inv) {
        *SNIP*
        var result = [];

        // Go through the array, only saving the items
        // that pass the validator function
        for ( var i = 0; i < elems.length; i++ )
            if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
                result.push( elems[i] );

        return result;
    },

Am I missing something here?

Cheers,
-js


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

Re: Change href of external links

by Ⓙⓐⓚⓔ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

upon testing filter with a function returns lots of items that aren't
nodes! But among them are the nodes in question.

                                $("a[@href^=http]:not(.thickbox)")
                                .filter(function(el){
                                        //alert(el.href);
                                        var m =  $.map(notthese,function(notthis){
                                                if (!el.nodeName) return false;
                                                alert(el.nodeName)
                                                if (el.href && el.href.indexOf(notthis)>-1) return true
                                        })
                                        //alert(m);
                                        return m;
                                })
                                .bind("click", function(){ return !window.open(this.href); });


On 12/14/06, Jonathan Sharp <jdsharp+jquery@...> wrote:

> On 12/14/06, Yehuda Katz <wycats@...> wrote:
> > If that was the case, jQuery("#main a").filter(function() { return false;
> }) would return nothing. But it doesn't. It returns the original group of
> elements. At least for me.
>
> Hm... I also tested this and filter didn't prune the list with my function
> returning false.
>
> Here's the filter function out of 1.0.3 which checks if t is a function and
> pushes the result of jQuery.grep(this, t) onto the stack.
>
> filter: function(t) {
>         return this.pushStack(
>             *SNIP*
>
>             typeof t == "function" &&
>             jQuery.grep( this, t ) ||
>
>             jQuery.filter(t,this).r, arguments );
>     },
>
> Now in the grep function it operates on the list of elements in elms. It
> calls the function we defined (fn) and if fn returns false it shouldn't add
> them to the result array.
>
> grep: function(elems, fn, inv) {
>         *SNIP*
>         var result = [];
>
>         // Go through the array, only saving the items
>         // that pass the validator function
>         for ( var i = 0; i < elems.length; i++ )
>             if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
>                 result.push( elems[i] );
>
>         return result;
>     },
>
> Am I missing something here?
>
> Cheers,
> -js
>
>
> _______________________________________________
> jQuery mailing list
> discuss@...
> http://jquery.com/discuss/
>
>
>


--
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
_______________________________________________
jQuery mailing list
discuss@...
http://jquery.com/discuss/

Re: Change href of external links

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Uh oh, I know what it is. It's a bug in pushStack.
 
If the last arg to pushStack is a function, it does .each(fn) with the filtered set but returns the original set. If there are two trailing function args, the first is treated as above. However, if the filtered set returned nothing (and thus fn was never called) it does .each(fn2) with the original set. Kind of like an if-then-else. It's never been documented but I think it will be eventually.
 
I think this should fix it:
 
 pushStack: function(a,args) {
  var fn = args && args.length > 1 && args[args.length-1];
  var fn2 = args && args.length > 2 && args[args.length-2];
 

From: discuss-bounces@... [mailto:discuss-bounces@...] On Behalf Of Jonathan Sharp
Sent: Thursday, December 14, 2006 2:31 PM
To: jQuery Discussion.
Subject: Re: [jQuery] Change href of external links

On 12/14/06, Yehuda Katz <wycats@...> wrote:
> If that was the case, jQuery("#main a").filter(function() { return false; }) would return nothing. But it doesn't. It returns the original group of elements. At least for me.

Hm... I also tested this and filter didn't prune the list with my function returning false.

Here's the filter function out of 1.0.3 which checks if t is a function and pushes the result of jQuery.grep(this, t) onto the stack.

filter: function(t) {
        return this.pushStack(
            *SNIP*

            typeof t == "function" &&
            jQuery.grep( this, t ) ||

            jQuery.filter(t,this).r, arguments );
    },

Now in the grep function it operates on the list of elements in elms. It calls the function we defined (fn) and if fn returns false it shouldn't add them to the result array.

grep: function(elems, fn, inv) {
        *SNIP*
        var result = [];

        // Go through the array, only saving the items
        // that pass the validator function
        for ( var i = 0; i < elems.length; i++ )
            if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
                result.push( elems[i] );

        return result;
    },

Am I missing something here?

Cheers,
-js


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

Re: Change href of external links

by agent2026 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/

Re: Change href of external links

by Jonathan Sharp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 12/15/06, agent2026 <adamd@...> wrote:

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);

Try: $(a[i]).bind('click', function(){ window.open(this.href.value) });

also for "pure" JavaScript the events are "onclick" instead of "onClick"...

                         }
                 };
                };
}
};

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

Re: Change href of external links

by Jonathan Sharp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 12/14/06, Dave Methvin <dave.methvin@...> wrote:
Uh oh, I know what it is. It's a bug in pushStack.
 
If the last arg to pushStack is a function, it does .each(fn) with the filtered set but returns the original set. If there are two trailing function args, the first is treated as above. However, if the filtered set returned nothing (and thus fn was never called) it does .each(fn2) with the original set. Kind of like an if-then-else. It's never been documented but I think it will be eventually.
 
I think this should fix it:
 
 pushStack: function(a,args) {
  var fn = args && args.length > 1 && args[args.length-1];
  var fn2 = args && args.length > 2 && args[args.length-2];

Has this been fixed in svn or has a bug ticket been opened?

Cheers,
-js

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

Re: Change href of external links

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Has this been fixed in svn or has a bug ticket been opened?
 
I just opened two tickets.
 
pushStack bug:
 
Document functions on destructive methods:

 

_______________________________________________
jQuery mailing list
discuss@...
http://jquery.com/discuss/
< Prev | 1 - 2 | Next >