Page 1 of 1

target="_blank" in Html Email

Posted: Sun Aug 25, 2024 8:57 am
by KenS
As discussed in another post, if I open an HTML email and a link contains target="_blank" the link will not work. You pointed out that SimpleBrowser2 does not support multiple browser windows - all ok.

Just before using Chromium.DefaultURL and Chromium.LoadURL I now scan the HTMLBody and change those links to remove the target. I then write the HTML to a temp file and pass that instead.

Questions:
What does DefaultURL do?

I tried Chromium.LoadString rather than a temp file but ended up with an empty window - any tricks I need to do?

A minor problem with the above approach is that is that (I think) if the second window also contains a target my program has control of Chromium at that stage to try and remove targets. Is there any easy way around this - I am not too fussed with it and can easily just live with it

Thanks

Ken

Re: target="_blank" in Html Email

Posted: Sun Aug 25, 2024 4:53 pm
by salvadordf
Hi,

DefaultURL is used to navigate to a URL automatically as soon as the browser is created.

A simple solution could be to cancel the navigation in OnBeforeBrowse and open the link in the current browser as described here :
https://www.briskbard.com/forum/viewtopic.php?t=2131

This solution works fine when the link uses a simple GET method but it fails with POST methods.

If you prefer removing the "target" attribute try executing this JavaScript code to remove all of them :

Code: Select all

for (const element of document.getElementsByTagName("a")) { element.removeAttribute("target"); }
Just execute that JS code as soon as the page is loaded. It shouldn't be necessary to create a copy in a temp file.

Re: target="_blank" in Html Email

Posted: Mon Aug 26, 2024 4:53 am
by KenS
salvadordf wrote: Sun Aug 25, 2024 4:53 pmIf you prefer removing the "target" attribute try executing this JavaScript code to remove all of them :
Yeah but I know more Delphi than JS :D
Copied your code, works great.
Just execute that JS code as soon as the page is loaded.
Chromium AfterCreated?

All works!

Ken

Re: target="_blank" in Html Email

Posted: Tue Aug 27, 2024 8:02 am
by salvadordf
KenS wrote: Mon Aug 26, 2024 4:53 am
Just execute that JS code as soon as the page is loaded.
Chromium AfterCreated?
That event is triggered too soon. Try executing the JavaScript code in TChromiumCore.OnLoadingStateChange when "isLoading" is false, or in TChromiumCore.OnLoadEnd.