user1937895
user1937895

Reputation: 39

Jquery. replace link if not found

I am a newbie and I have inherited some nasty jquery code. I am trying to find a way so that if an error is found then it replaces the string with another string. This is the original code..

    W.load = function (b) {
    var c, d, g = W.prep; S = ! 0, Q = ! 1, O = x[P], b || _(a.extend(J, a.data(O, e))), ba(l), ba(h, J.onLoad), J.h = J.height? Z(J.height, "y") - M - K: J.innerHeight && Z(J.innerHeight, "y"), J.w = J.width? Z(J.width, "x") - N - L: J.innerWidth && Z(J.innerWidth, "x"), J.mw = J.w, J.mh = J.h, J.maxWidth &&(J.mw = Z(J.maxWidth, "x") - N - L, J.mw = J.w && J.w < J.mw? J.w: J.mw), J.maxHeight &&(J.mh = Z(J.maxHeight, "y") - M - K, J.mh = J.h && J.h < J.mh? J.h: J.mh), c = J.href, V = setTimeout(function () {
        B.show()
    },
    100), J.inline?(Y().hide().insertBefore(a(c)[0]).one(l, function () {
        a(this).replaceWith(z.children())
    }), g(a(c))): J.iframe? g(" "): J.html? g(J.html): $(c)?(a(Q = new Image).addClass(f + "Photo").error(function () {
        //J.title = ! 1, g(Y("Error").text("This image could not be loaded"))
        J.title = ! 1, a(this).href.replace('http://www.old.com','http://www.new.com');
    }).load(function () {
        var a; Q.onload = null, J.scalePhotos &&(d = function () {
            Q.height -= Q.height * a, Q.width -= Q.width * a
        },
        J.mw && Q.width > J.mw &&(a =(Q.width - J.mw) / Q.width, d()), J.mh && Q.height > J.mh &&(a =(Q.height - J.mh) / Q.height, d())), J.h &&(Q.style.marginTop = Math.max(J.h - Q.height, 0) / 2 + "px"), x[1] &&(P < x.length - 1 || J.loop) &&(Q.style.cursor = "pointer", Q.onclick = function () {
            W.next()
        }), m &&(Q.style.msInterpolationMode = "bicubic"), setTimeout(function () {
            g(Q)
        },

I have changed the code to this..

J.title = ! 1, a(this).href.replace('http://www.old.com','http://www.new.com');

But its not working. Ggr! Any help would be greatly appriciated. :)

UPDATE*

The code above is the complete code for the section I am working in..

UPDATE #2

This answer works but I need to do...

a(this).attr('src', function(i, current){ 
 if ( i === 'http://www.old.com'){
 return current.replace('http://www.old.com','http://www.new.com');
 }
 else if ( i === 'http://www.older.com'){
 return current.replace('http://www.older.com','http://www.new.com');
}
 else ();
 })

This isn't working!! Help!!

Upvotes: 1

Views: 369

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196002

Try

this.href.replace('http://old.com/' , 'http://new.com/');

If you want to change the actual attribute to a new one on the current element then

this.href = this.href.replace('http://old.com/' , 'http://new.com/');

If you are inside a handler that refers to an <a> element then this refers to that object and you do not need to make a jquery object out of it, to access its properties.. so this.href will refer directly to the href property of the link.


Disclaimer: That is obfusated/encoded script and you should better find the original from which this was produced.. This code makes no sense and our suggestion are really in the blind..

From a first look it seems the this is an image and not a link, so if you want to show a new image then you would need to change the src attribute and not the href which does not exist on images..

a(this).attr('src', function(i, current){ return current.replace('http://www.old.com','http://www.new.com');})

But you should also do a console.log(a.fn.jquery) in there to make sure that a is a reference to jQuery

Upvotes: 1

Dominic Green
Dominic Green

Reputation: 10258

Do you need to do a replace ? If you just want to change the url then you can just do.

J.title = ! 1, $(this).attr('href', 'http://new.com/*');

Which will replace the current url to the new url given.

Upvotes: 0

Related Questions