Link to home
Start Free TrialLog in
Avatar of Mark Drelinger
Mark DrelingerFlag for United States of America

asked on

alter javascript to drop off unwanted characters from current result.

I have a javascript that collects and manipulates the url.
It works well, but on occasion it gathers parameters that I want to drop off.
Ideally, I just want three characters after the decimal point.
Here is an example of a bad result:
          LoanerPolicyUpdateMgr.asp%3FvarID%3D1272
I want it to be
          LoanerPolicyUpdateMgr.asp
Here is my script:
function GetURL(){
            var AppURL = "";
    if (window.location.href.indexOf('=') > -1) {
              AppURL = window.location.href.split('?')[1];
        }
          AppURL = AppURL.replace(/accessdenied=/gi, "");
          AppURL = AppURL.replace(/%2F/gi, "");
          AppURL = AppURL.replace(/%2E/gi, "");
          AppURL = AppURL.replace(/webapps20/gi, "");
          AppURL = AppURL.replace(/asp/gi, ".asp");
            
            var str =AppURL;
              var res = str.substring(0, 50);
             document.getElementById("AccessCode_AppURL").value = res;            
}
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Create an html file with this code
<script>
console.log(window.location);
</script>

Open in new window

Open this page in your browser
Press F12 to go to your console and make sure the Console tab is selected
You should see Location ... in the console - click it to expand its properties

You should see something like this
Location https://www.experts-exchange.com/questions/29142994/alter-javascript-to-drop-off-unwanted-characters-from-current-result.html
​assign: function assign()
​hash: ""
​host: "www.experts-exchange.com"
​hostname: "www.experts-exchange.com"
​href: "https://www.experts-exchange.com/questions/29142994/alter-javascript-to-drop-off-unwanted-characters-from-current-result.html"
​origin: "https://www.experts-exchange.com"
​pathname: "/questions/29142994/alter-javascript-to-drop-off-unwanted-characters-from-current-result.html"
​port: ""

Open in new window

The pathname property contains the path to your script. If you just want the file name then

var filename = window.location.pathname.split('/').pop();

Open in new window

Avatar of Mark Drelinger

ASKER

My challenge is that I'm looking for a portion of the redirect url
 My URL
https://www.mywebsite.com/WebApps2.0/LoginReturn2.asp?accessdenied=%2FWebApps2%2E0%2FContactManagementSearch%2Easp?a=johnsmith

 In this example, my current code gives me
 ContactManagementSearch.asp?a=johnsmith

 The result I need is ContactManagementSearch.asp
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes,
url.split('=')[1].split('?')[0].split('/').pop();
worked well.  Thanks and sorry for the misleading question.
You are welcome, no problem - glad you got sorted.