ZachCase

removeRLParameter.js

Mar 2nd, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function removeURLParameter(url, parameter) {
  2.     //prefer to use l.search if you have a location/link object
  3.     var urlparts= url.split('?');  
  4.     if (urlparts.length>=2) {
  5.         var prefix= encodeURIComponent(parameter)+'=';
  6.         var pars= urlparts[1].split(/[&;]/g);
  7.  
  8.         //reverse iteration as may be destructive
  9.         for (var i= pars.length; i-- > 0;) {    
  10.             //idiom for string.startsWith
  11.             if (pars[i].lastIndexOf(prefix, 0) !== -1) {
  12.                 didRemoveURLParameter = true;
  13.                 pars.splice(i, 1);
  14.             }
  15.         }
  16.  
  17.         url= urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : "");
  18.         return url;
  19.     } else {
  20.         return url;
  21.     }
  22. }
  23.  
  24. // We want to remove the `rl` parameter from the URL if it is present so that the RedirectURL is properly handled.
  25. // This also allows us not to rely on forcing users to come to the form(s) via formula - url fields, which allows for more native navigation of the apps.
  26. var url = window.location.href,
  27.     didRemoveURLParameter = false,
  28.     urlParams = new URLSearchParams(window.location.search),
  29.     cleanedURL = removeURLParameter(url, "rl");
  30.  
  31. // Check to make sure we are editing the record
  32. var isEditing = false;
  33. if(urlParams.has("a") && urlParams.get("a") == "er") isEditing = true;
  34.  
  35. if(didRemoveURLParameter && isEditing) window.location.replace(cleanedURL, false);
Advertisement
Add Comment
Please, Sign In to add comment