';
var s=document.createElement('script');
s.src='https://book.escapervrentals.com/widget/searchform.js?element_id='+encodeURIComponent(id)+'&affiliate='+encodeURIComponent('escapervrentals')+'&target='+encodeURIComponent('_blank')+'&pickup_country='+encodeURIComponent(pc)+'&pickup_location='+encodeURIComponent(pickup.id)+'&dropoff_location=&country=&language=en';
wrap.appendChild(s);
}
function updateWidgetCopy(info,location){
var title=document.querySelector('[data-erv-geo-widget-title]');
if(title&&info&&location)title.textContent='Book '+info.term+' near '+location+' — on Escape RV Rentals';
var note=document.querySelector('[data-erv-geo-widget-note]');
if(note&&info&&location)note.textContent='Showing options near '+location+', '+info.label+'. Review insurance and cancellation before you confirm — checkout stays on our site.';
document.querySelectorAll('.mhr-load-msg').forEach(function(m){
if(location)m.textContent='Finding fleet options near '+location+'…';
});
}
function hideGeoBar(){
var bar=document.getElementById('erv-geo-bar');
if(bar)bar.style.display='none';
}
function geoBarDismissBtn(){
return '
× ';
}
function initGeoBarDismiss(){
if(document.body&&document.body.dataset.ervGeoDismiss)return;
if(document.body)document.body.dataset.ervGeoDismiss='1';
document.addEventListener('click',function(e){
var t=e.target;
if(!t||!t.getAttribute||t.getAttribute('data-erv-geo-dismiss')!=='1')return;
hideGeoBar();
});
}
function showGeoBarFallback(msg){
var bar=document.getElementById('erv-geo-bar');
if(!bar)return;
bar.innerHTML=msg+geoBarDismissBtn();
bar.style.display='flex';
}
function showCrossCountryBanner(geo,info,location){
var bar=document.getElementById('erv-geo-bar');
if(!bar||!info)return;
var path=window.location.pathname;
var hub=(info.hub||'').split('/').filter(function(p){return p;})[0]||'';
if(isHome(path)||path.startsWith('/'+hub)||path==='/rv-rental-near-me/')return;
var slug=CITY_MAP[String(geo.city||'').toLowerCase().trim()];
var cityLink=slug&&location?' ·
'+location+' guide ':'';
bar.innerHTML=info.flag+' Browsing from
'+info.label+' ? Search fleet inventory below or
open '+info.label+' guides '+cityLink+'. '+geoBarDismissBtn();
bar.style.display='flex';
}
function allMhrWraps(){
var auto=document.querySelectorAll('[data-erv-mhr-auto],[data-erv-mhr-defer],[data-erv-mhr-geo],[data-mhr-fallback]');
if(auto.length)return auto;
var legacy=[];
document.querySelectorAll('[id^="VehicleSearchForm"]').forEach(function(el){
if(el.hasAttribute('data-erv-mhr-skip'))return;
var wrap=el.closest('.mhr-widget-wrap')||el.parentElement;
if(!wrap||wrap.hasAttribute('data-erv-mhr-skip'))return;
legacy.push(wrap);
});
return legacy;
}
function geoFailFallback(){
var pageCtx=pageCityContext();
if(pageCtx){
updateMhrCtas(pageCtx.label,pageCtx.location,pageCtx.iso);
hideGeoBar();
return;
}
loadDefaultMhr();
updateMhrCtas('United States',DEFAULT_MHR_CITY.US||'Los Angeles','US');
showGeoBarFallback('🌍 Location unavailable.
Book on Escape RV Rentals — book securely on our site.');
}
function localizeAllPages(geo){
var path=window.location.pathname;
var pageCtx=pageCityContext();
var widgets=allMhrWraps();
if(pageCtx){
if(widgets.length){
widgets.forEach(function(wrap){
if(wrap.hasAttribute('data-erv-mhr-skip'))return;
if(wrap.hasAttribute('data-erv-mhr-city-lock')||wrap.hasAttribute('data-erv-mhr-fixed'))return;
loadMhrWidget(wrap,pageCtx.label,pageCtx.location);
});
updateWidgetCopy(pageCtx.info,pageCtx.location);
}
updateMhrCtas(pageCtx.label,pageCtx.location,pageCtx.iso);
hideGeoBar();
if(geo&&geo.country_code&&geo.country_code!==pageCtx.iso){
var visitorInfo=COUNTRY_MAP[geo.country_code];
var visitorLoc=mhrLocation(geo.city,geo.country_code);
if(visitorInfo)showCrossCountryBanner(geo,visitorInfo,visitorLoc);
}
return true;
}
var info=geo&&geo.country_code?COUNTRY_MAP[geo.country_code]:null;
var location=geo&&geo.country_code?mhrLocation(geo.city,geo.country_code):'';
if(isHome(path)&&info){
updateMhrCtas(info.label,'',geo.country_code);
var slug=CITY_MAP[String(geo.city||'').toLowerCase().trim()];
var homeLoc=location||DEFAULT_MHR_CITY[geo.country_code]||'';
var cityLink=slug&&homeLoc?' ·
'+homeLoc+' guide ':'';
var bookUrl=mhrBookUrl(info.label,homeLoc,geo.country_code);
showGeoBarFallback(info.flag+' Browsing from
'+info.label+' ?
Explore '+info.label+' guides '+cityLink+' ·
Book on Escape RV Rentals ');
return true;
}
if(widgets.length&&info&&location){
widgets.forEach(function(wrap){
if(wrap.hasAttribute('data-erv-mhr-skip'))return;
loadMhrWidget(wrap,info.label,location,true);
});
updateWidgetCopy(info,location);
updateMhrCtas(info.label,location,geo.country_code);
hideGeoBar();
showCrossCountryBanner(geo,info,location);
return true;
}
if(widgets.length&&info){
widgets.forEach(function(wrap){
if(wrap.hasAttribute('data-erv-mhr-skip'))return;
loadMhrWidget(wrap,info.label,location||'');
});
updateMhrCtas(info.label,location||'',geo.country_code);
}
loadDefaultMhr();
updateMhrCtas('United States',DEFAULT_MHR_CITY.US||'Los Angeles','US');
showGeoBarFallback('🌍 We could not detect your nearest depot.
Book on Escape RV Rentals — secure checkout on our site.');
return false;
}
function onGeo(geo){
var path=window.location.pathname;
if(path==='/search/'||path==='/search'){
window.location.replace('/');
return;
}
if(!geo||!geo.country_code){
geoFailFallback();
return;
}
try{sessionStorage.setItem(GEO_KEY,JSON.stringify(geo));}catch(e){}
if(path==='/rv-rental-near-me/'||path==='/rv-rental-near-me'){
var city=(geo.city||'').toLowerCase().trim();
var slug=CITY_MAP[city];
if(slug){window.location.replace(CITY_PATH_MAP[slug]||'/');return;}
var info=COUNTRY_MAP[geo.country_code];
if(info){window.location.replace(info.hub);return;}
window.location.replace('/');
return;
}
localizeAllPages(geo);
}
function loadDefaultMhr(){
var wraps=allMhrWraps();
wraps.forEach(function(wrap){
if(wrap.hasAttribute('data-erv-mhr-home'))return;
wrap.querySelectorAll('script[src*="widget/searchform.js"]').forEach(function(s){s.remove();});
loadMhrWidget(wrap,'United States',DEFAULT_MHR_CITY.US||'Los Angeles',true);
});
}
function bootDeferredMhr(){
allMhrWraps().forEach(function(wrap){
if(wrap.hasAttribute('data-erv-mhr-home'))return;
if(wrap.hasAttribute('data-erv-mhr-skip')||wrap.hasAttribute('data-erv-mhr-city-lock')||wrap.hasAttribute('data-erv-mhr-fixed'))return;
if(wrap.querySelector('script[src*="widget/searchform.js"]'))return;
if(wrap.hasAttribute('data-erv-mhr-defer')||wrap.hasAttribute('data-erv-mhr-auto')){
loadMhrWidget(wrap,'United States',DEFAULT_MHR_CITY.US||'Los Angeles',true);
}
});
}
var bootCtx=pageCityContext();
if(bootCtx&&!document.body.getAttribute('data-city'))updateCityStickyBookBar(bootCtx.location);
initGeoBarDismiss();
bootDeferredMhr();
function runGeoFetch(){
// Try cache first
try{
var cached=sessionStorage.getItem(GEO_KEY);
if(cached){onGeo(JSON.parse(cached));return;}
}catch(e){}
// Fetch geo with 6s timeout — deferred so it does not compete with LCP
var ctrl=typeof AbortController!=='undefined'?new AbortController():null;
var timer=ctrl?setTimeout(function(){ctrl.abort();},6000):null;
fetch('https://ipapi.co/json/',ctrl?{signal:ctrl.signal}:{})
.then(function(r){return r.json();})
.then(function(d){clearTimeout(timer);onGeo(d);})
.catch(function(){
clearTimeout(timer);
geoFailFallback();
});
}
var deferMs=document.body&&document.body.getAttribute('data-city')?8000:3000;
function scheduleGeo(){
if(typeof requestIdleCallback==='function'){
requestIdleCallback(function(){setTimeout(runGeoFetch,deferMs);},{timeout:deferMs+2000});
}else{
setTimeout(runGeoFetch,deferMs);
}
}
scheduleGeo();
})();
});