You are here

seoLinksOnPage

Used by the following Containers: 
Variable Type: 
Custom Javascript
Number of links as an array of objects on the page
	function() {
    var links = document.body.getElementsByTagName("a");
  	var linksNumber = links.length;
  	var seoHostname = {{seoHostname}}; // to check if the link is internal
	var seoMetaRobotsFollow = {{seoMetaRobotsFollow}};
	var seoUrlCurrent = {{seoUrlCurrent}};
	var seoLinksOnPage = [];
  	
  	var i;
    for ( i = 0; i < linksNumber; i++ ) {
      	
      	// is linkDestination internal or External ?
      	var linkHref = links[i].href.split('#')[0];
      	
      	if (linkHref != '' &&
            linkHref != undefined &&
            linkHref != seoUrlCurrent &&
            linkHref != seoUrlCurrent + '/' &&
            linkHref.indexOf('http') > -1) {
            
          	// will need to include the link type first
          	var linkAnchor = links[i].innerText;


            // is the link followed ?
          	if ( seoMetaRobotsFollow == 'nofollow') {
              	// remember that if nofollow is applied to the metaRobots Tag, all links are then nofollow
              	var linkFollowed = nofollow;
            } else {
              	// otherwise I look at the attribute itself
                var linkRelAttribute = links[i].rel;
                if (linkRelAttribute.indexOf('nofollow') != -1) {
                    linkFollowed = 'nofollow'
                } else {
                    linkFollowed = 'follow'
                }
            }
			
          	// if the target is empty the target is _self by default
            var linkTarget = links[i].target;
            if (linkTarget == '') {
                linkTarget = '_self'
            }
            // is linkDestination internal or External ?
            if ( linkHref.indexOf(seoHostname) == -1) {
            	var linkDestination = 'external'
            } else {
            	var linkDestination = 'internal'
            }


            seoLinksOnPage.push({'href': linkHref,
                                 'anchor': linkAnchor,
                                 'destination': linkDestination,
                                 'rel': linkFollowed,
                                 'target': linkTarget
                                })
        }
    }
  	return seoLinksOnPage;
}