// Is there jQuery? if (typeof jQuery == 'undefined') { alert("De DS Verzekeringen plugin needs the jQuery library to function"); } /** * Receiver * * Initialize an event listener on the window * that listens for messages send by the iframe. * * When the message event is triggered the iframe * height is ajusted. In the case that the browser * does not support the postMessage functionality * the height of the iframe is set to defaultHeight. * * @version 1.1 * @date 11/05/2015 * @author Frank Bonnet */ (function($) { // Start and fallback height var DefaultHeight = 1000; // Read script src if ($("script#receiver").length == 1) { Script = $("script#receiver"); Script.removeAttr("id"); // Next script keeps working Script = Script[0]; } else { Scripts = document.getElementsByTagName('script'); Script = Scripts[Scripts.length - 1]; } ScriptSrc = Script.src; /** * Read a named param from the url * * @var param ParamName */ jQuery.UrlParam = function(ParamName, QueryString){ var results = new RegExp('[\?&]' + ParamName + '=([^&#]*)').exec(QueryString); if (results==null){ return null; } else{ return results[1] || 0; } } /** * Read a named param from the url * * @var param DefaultValue */ jQuery.ReadUrlParam = function(ParamName, DefaultValue) { var Response; var ParamValue = decodeURIComponent(jQuery.UrlParam(ParamName, window.location.href)); if (null != ParamValue && 'null' != ParamValue) { Response = ParamValue; } else { Response = DefaultValue; } return Response; } /** * Read a named param from the script src * * @var param DefaultValue */ jQuery.ReadScriptParam = function(ParamName, DefaultValue) { var Response; var ParamValue = decodeURIComponent(jQuery.UrlParam(ParamName, ScriptSrc)); if (null != ParamValue && 'null' != ParamValue) { Response = ParamValue; } else { Response = DefaultValue; } return Response; } /** * Read a named param from the url * or else from the script source * * @var param DefaultValue */ jQuery.ReadParam = function(ParamName, DefaultValue) { var Response; var ParamValue; // Try to read from url ParamValue = decodeURIComponent(jQuery.UrlParam(ParamName, window.location.href)); if (null != ParamValue && 'null' != ParamValue) { Response = ParamValue; } if (null == Response) { // Try to read from script src ParamValue = decodeURIComponent(jQuery.UrlParam(ParamName, ScriptSrc)); if (null != ParamValue && 'null' != ParamValue) { Response = ParamValue; } else { // Not in script src and not in url Response = DefaultValue; } } return Response; } /** * Get the default height from url, script src or back-up server set */ jQuery.GetDefaultHeight = function() { var Response; // Try to read from url ParamValue = decodeURIComponent(jQuery.ReadParam("DefaultHeight", DefaultHeight)); if (null != ParamValue && 'null' != ParamValue) { Response = ParamValue; } return Response; } /** * Resize the iframe * * @var param frame height */ jQuery.ResizeFrame = function(height) { jQuery("#insurance_frame").height(height); } /** * Scroll to the top of the iframe */ jQuery.ScrollFrameTop = function() { jQuery('html, body').animate({ scrollTop: jQuery("#insurance_frame").offset().top }); } /** * Open a link * * @var location to go to * @var target _blank|_self */ jQuery.OpenLink = function(location, target) { if (target == "_blank") { window.open(location); } else { window.location = location; } } /** * Called by the message event lisener * * Checks if event.origin is allowed in the * AcceptedOrigins array; */ jQuery.ReceiveMessage = function(event) { if (event.data.id == "ResizeFrame") { jQuery.ResizeFrame(parseInt(event.data.height)); } else if (event.data.id == "OpenLink") { jQuery.OpenLink(event.data.location, event.data.target); } else if (event.data.id == "ScrollFrameTop") { jQuery.ScrollFrameTop(); } } /** * Read the AdFrameSource param from the url and * append the dealerid if not present in the querystring * * @var param DefaultValue */ jQuery.GetFrameSource = function(DefaultValue) { var FrameSource = jQuery.ReadUrlParam("FrameSource", DefaultValue); var Params = { Theme: jQuery.ReadParam("Theme", null) }; var _stylesheet = jQuery.ReadParam("Stylesheet", null); if (null == _stylesheet) { Params.StyleSheet = _stylesheet; } var _apiname = jQuery.ReadParam("ApiName", null); if (null != _apiname) { Params.ApiName = _apiname; } var _dealerId = jQuery.ReadParam("DealerId", null); if (null != _dealerId) { Params.DealerId = _dealerId; } if (null == jQuery.UrlParam("Layout", FrameSource)) { Params.Layout = jQuery.ReadScriptParam("Layout", "Frame"); } if (null == jQuery.ReadParam("LicensePlate", FrameSource)) { Params.LicensePlate = jQuery.ReadScriptParam("LicensePlate", ""); } FrameSource += (FrameSource.indexOf('?') >= 0 ? '&' : '?') + jQuery.param(Params); return FrameSource; } // Write iframe document.write(''); /** * If browser does can't post a message fallback to * the default height (child cannot post messages). * * Otherwise add an eventlistener to the window that * listens for the ReceiveMessage event. */ if (!window["postMessage"]) { jQuery.ResizeFrame(DefaultHeight); } else { if (window.addEventListener) { // Standard window.addEventListener("message", jQuery.ReceiveMessage); } else { // Support ie8 window.attachEvent("onmessage", jQuery.ReceiveMessage); } } })(jQuery);