Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // set your AFFILIATE IDs here. Leave blank any you do not have an account for.
- var arrAffiliates = {
- 'co.uk' : '',
- 'com' : '',
- 'de' : '',
- 'fr' : '',
- 'ca' : '',
- 'jp' : ''
- }
- /**
- * search for any AMAZON product links and replace with a referral link to the user's local Amazon site
- * @author Pete Williams
- * @url http://petewilliams.info/blog/2009/07/javascript-amazon-associate-link-localiser/
- */
- function checkAmazonLinks() {
- // set required VARIABLES
- var strTld;
- var objRegexAsin = new RegExp( '\/([A-Z0-9]{10})' );
- if ( typeof google != "undefined" && google.loader.ClientLocation != null ) {
- var strCountry = google.loader.ClientLocation.address.country_code;
- // get domain TLD from country code
- switch ( strCountry ) {
- case 'GB':
- case 'IE':
- strTld = 'co.uk';
- break;
- case 'AT':
- strTld = 'de';
- default:
- // by default, TLD is lowercase of country code. if no associate, then use .com
- strTld = ( arrAffiliates[strCountry.toLowerCase()] ? strCountry.toLowerCase() : 'com' );
- break;
- }
- strAffiliateId = getAffiliateId( strTld );
- }
- // get all LINKS
- var arrLinks = document.getElementsByTagName( 'a' );
- // LOOP through
- for ( i=0; i<arrLinks.length; i++ ) {
- // is it an AMAZON link
- var intIndex = arrLinks[i].href.toLowerCase().indexOf( 'amazon.' );
- if ( intIndex > 0) {
- // find ASIN
- var arrResults = objRegexAsin.exec( arrLinks[i].href );
- if ( arrResults ) {
- // REPLACE URI
- var strAsin = arrResults[1];
- // check location has worked
- if ( typeof(strAffiliateId) == "undefined" ) {
- strTldToUse = getOriginalTld( arrLinks[i].href );
- strAffiliateIdToUse = getAffiliateId( strTldToUse );
- } else {
- strTldToUse = strTld;
- strAffiliateIdToUse = strAffiliateId;
- }
- arrLinks[i].href = 'http://www.amazon.' + strTldToUse + '/exec/obidos/ASIN/' + strAsin + '/' + strAffiliateIdToUse;
- }
- }
- }
- }
- function getAffiliateId( strTLD ) {
- return( arrAffiliates[strTLD] ? arrAffiliates[strTLD] : arrAffiliatesSpares[strTLD] );
- }
- function getOriginalTld( strUrl ) {
- var c = new RegExp( "amazon.(.*?)/" );
- arrTldActual = c.exec( strUrl );
- return arrTldActual[1];
- }
- // initialise on page LOAD
- if (window.addEventListener){
- window.addEventListener('load',checkAmazonLinks,false); //W3C
- } else{
- window.attachEvent('onload',checkAmazonLinks); //IE
- }
- // these are the author's affiliate IDs. They will only be used for localities you do not provide an ID for.
- var arrAffiliatesSpares = {
- 'co.uk' : 'pcrev05',
- 'com' : 'petewill-20',
- 'de' : 'petewill05-21',
- 'fr' : 'petewill-21',
- 'ca' : 'petewill00-20',
- 'jp' : 'petewill-22'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement