Ionic 2 store string from URL in variable

I currently have an inappbrowser in my application that loads up a specific url. Upon signing in, they get redirected to a URL that contains a code I want to store in a variable. The redirect would look like this:

https://REDIRECT_URL/?code=CODE_I_WANT_TO_GRAB&state=STATE

I would like to utilize something to wait for that link to load in, and then grab the “code” and store it to a variable in local storage. My inappbrowser is as follows:

      urlsp = "URL_THAT_I_FIRST_LOAD";
      let browser = new InAppBrowser(urlsp, '_blank', 'location=yes,zoom=yes,enableViewportScale=yes');
      browser.on("loadstart").subscribe(() => {

        //get token from url redirect
        var auth_token = this.getQueryVariable("code");
        localStorage.setItem('auth_token', auth_token);
      });

The function getQueryVariable is as follows:

  getQueryVariable(codeVar){
    //pull auth_token from url on redirect
    var query = window.location.search.substring(1);
    var vars = query.split("&");

    for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split('=');
      if(pair[0] = codeVar){return pair[1];}
    }
  }

This function is not giving me the result I desire. Any help would be appreciated!

in InAppBrowser “loadstart” function first parameter type is InAppBrowserEvent. You must use this parameter to get url.

browser.on("loadstart").subscribe((event: InAppBrowserEvent) => {
            let url = event.url;
        });

If you use the function. You must set a parameter like url.

browser.on("loadstart").subscribe((event: InAppBrowserEvent) => {
           var auth_token = this.getQueryVariable(event.url, "code");
           localStorage.setItem('auth_token', auth_token);
        });
 getQueryVariable(url, codeVar){
    // do your staff
  }