Jump to content

Update Browser URL Address Bar without reloading the page - help


andyhill

Recommended Posts

I have MainModule BeforeLogin code decoding the URL parameters and setting appropriate flags - all good.

After acting on these flags in the MainForm I want to remove the parameters from the Browser URL Address Bar without reloading the page, my code below while executing does not achieve that result, please advise how - Thanks.

eg.
https://myDomain.com/?doSomething=log --> https://myDomain.com

 

procedure TMainForm.UniFormShow(Sender: TObject);
begin
  if FirstTimeFlag = True then begin
    FirstTimeFlag:= False;
    if UniMainModule.RequestEmailLog = True then begin
      UniMainModule.RequestEmailLog:= False;
      MainForm.ShowMask();
      UniServerModule.EmailLog(EmailAddress);
      MainForm.HideMask;

      UniSession.AddJS('var newurl = MainForm.window.location.protocol + "//" + MainForm.window.location.host + MainForm.window.location.pathname; '+
                       'MainForm.window.history.pushState({path: newurl}, '', newurl); ');

    end;
  end;
end;

 

Link to comment
Share on other sites

I want to do the same ;-)

Simply delete the post parameter does not result in new url.

In my case: You can start the loginscreen with some parameters. After login I want to show the mainform without the login options parameters in the url

Link to comment
Share on other sites

Hello, try this.

https://www.30secondsofcode.org/articles/s/javascript-modify-url-without-reload

 

procedure TMainForm.UniFormActivate(Sender: TObject);
var

EnableAutoLog : Boolean;
begin
 If (TRIM (TUniGUISession(UniSession).UniApplication.Parameters.Values ['login']) <> '')
 and (EnableAutoLog) then begin
    //replace URL
    UniSession.AddJS(
            // Current URL: UniSession.ARequest.Referer
            'const nextURL = ''' + StringReplace (UniSession.ARequest.Referer,'login='  + TUniGUISession(UniSession).UniApplication.Parameters.Values ['login'],'',[rfReplaceAll,rfIgnoreCase]) + ''';'
            + 'const nextTitle = ''' + UniServerModule.Title + ''';'
            + 'const nextState = { additionalInformation: ''Updated the URL with JS'' };'
            // This will create a new entry in the browser's history, without reloading
            + 'window.history.pushState(nextState, nextTitle, nextURL);'
            // This will replace the current entry in the browser's history, without reloading
            + 'window.history.replaceState(nextState, nextTitle, nextURL);'
            );

 end;

But, You must know this will replace only URL, on Google Console will be seen all history as clear text !!!

Link to comment
Share on other sites

The suggestion to use UniSession.ARequest.Referer fails as it is blank.

My original code is fine and it was only missing the window.history.replaceState call.

    //
    UniSession.AddJS('const nextURL   = window.location.protocol + "//" + window.location.host + window.location.pathname; '+
                     'const nextTitle = ''' + UniServerModule.Title + '''; '+
                     'const nextState = {additionalInformation: ''Updated the URL with JS'' }; '+
                     'window.history.pushState(nextState, nextTitle, nextURL); '+
                     'window.history.replaceState(nextState, nextTitle, nextURL); ');
    //

Thank you

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...