anavdesign
4/22/2016 - 3:32 AM

JS: Back Button

JS: Back Button

/*
 * Back Button
 */

/***********************************************************
 ***********************************************************
  
 Usage:
 $('#elm')backBtn({ // Append button to this element
     btnText: 'Back Button', // Button Text
     btnID: 'back-button', // Button Wrapper ID
     btnClass: 'btn btn-primary btn-sm' // Button Class Names
 });

 ***********************************************************
 ***********************************************************/

(function ($) {
	$.fn.backBtn = function(options) {

		// Options
		var settings = $.extend({
				btnText: 'Back',			// Button Text
				btnID: 'js-back-button',	// Button Class
				btnClass: ''				// Button Class
			}, options);

		var	backBtnHostName = $(location).attr('host'), 	// Site Domain
			backBtnReferrer =  document.referrer; 			// Refurring URL

		// Check if reffering page is on this site
		if(backBtnReferrer.indexOf(backBtnHostName) >= 0) {
			// Add Back Button
			this.append('<div id=' + settings.btnID + ' class=js-back-btn><a href="' + backBtnReferrer + '" >' + settings.btnText + '</a></div>');
		}

		// Add Button Class
		if (settings.btnClass !== '') {
			$('#' + settings.btnID + ' a').addClass(settings.btnClass);
		}

		// console.log('Domain Name:' + backBtnHostName);
		// console.log('Referer:' + backBtnReferrer);

		return this;

	};
}(jQuery));