javascript - Rewriting a URL if it 404's -


first timer here nice :3.

i attempting write jquery function rewrites amazon url's include affiliate tags, similar stackexchange twist.

the main differences attempting user closest amazon store - e.g. amazon.de - german visitors. due amazon's asin's differing in countries first want check new link, if 404's don't want direct visitor there [1]

here code selects links amazon.com, grabs asin number , writes shortlink product including affiliate tag.

var tld_table = {'gb' : ".co.uk",'de' : ".de",'cn' : ".cn",'au' : ".ca",'it' : ".it",'fr' : ".fr",'ca' : ".ca",'jp' : ".jp",}; var country = $.cookie("countrycode");  //$.cookie http://plugins.jquery.com/files/jquery.cookie.js.txt var tld = tld_table[country] || '.com'; var regex = regexp("http://www.amazon.com/([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})"); $('a[href*="amazon.com"]').each(function(){ var url = $(this).attr('href'); m = url.match(regex);     if (m) { //if asin found     var asin = m[4];     var shorturl = "http://www.amazon"+tld+"/dp/" + asin + "?tag="+ afftag[tld];         //http test 404         //if 404 not rewrite         //else $(this).attr('href',shorturl);     } }); 

this works fine , re-write url's when introduce ajax equation script fails rewrite url's.

edit

$('a[href*="amazon.com"]').each(function(){     var url = $(this).attr('href');     m = url.match(regex);     if (m) { //if asin found http://www.amazon.com/dp/b003dz1y8q/?tag=derp     var asin = m[4];     var ajaxcall = $.get('asin.php?asin='+asin+'&tld='+tld+'&tag='+afftags[tld], function(data) {         var newlink = data;         console.log('new link: '+newlink)         $(this).attr('href',newlink); //does not rewrite     })     ajaxcall.success(function() {     if(newlink != '404'){          $(this).attr('href',newlink);//does not rewrite         }     })       }  }); 

above code attempting use currently, asin.php builds & requests new link, opens using php's curl , returns either new link or '404'.

i think $(this) failing reference link correctly, have no idea why.

the error says all: is not allowed access-control-allow-origin

it means javascript not allowed retrieve url outside of domain. can fix rewriting ajax request local php script checks url.

it has http://en.wikipedia.org/wiki/same_origin_policy


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -