dvasudevan
New Creator

Opening link in browser with FS_BUTTON

Hello all,

Any idea how I can open an external link in a browser with FS_BUTTON ?

Thanks

0 Kudos
2 Replies
cpe
Elite Observer

Hey there,

have you tried calling a script with an FS_BUTTON and opening your URL from there?

Cheers
Connz

0 Kudos
ChKo
Elite Observer

Hi,

here is a solution that should serve as inspiration:

  <FS_BUTTON
    name="pt_openUrl_button"
    hFill="yes"
    icon="media:question_mark"
    noBreak="no"
    onClick="script:sc_openurlinbrowser"
    style="firstspirit"
    useLanguages="no">
    <LANGINFOS>
      <LANGINFO lang="*" label="Open link" description="Help"/>
      <LANGINFO lang="DE" label="Öffne Link" description="Hilfe"/>
    </LANGINFOS>
    <PARAMS>
      <PARAM name="p_url">https://www.your-target-domain.com/</PARAM>
    </PARAMS>
  </FS_BUTTON>

 

import de.adesso.firstspirit.template.AbstractExecutable;
import de.espirit.firstspirit.access.BaseContext;
import de.espirit.firstspirit.access.ClientScriptContext;
import de.espirit.firstspirit.agency.OperationAgent;
import de.espirit.firstspirit.webedit.server.ClientScriptOperation;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class OpenUrlInBrowserExecutable extends AbstractExecutable<ClientScriptContext> {

    private final String PARAM_URL = "p_url";

    @Override
    public Object execute(){
        String url = getStringFromParameters(PARAM_URL);
        //ContentCreator Case
        if(context.is(BaseContext.Env.WEBEDIT)){
            openWindowWebEdit(url);
        }
        //SiteArchitect Case
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (URISyntaxException | IOException e) {
                context.logError("Could not open URL '" + url + "' !", e);
            }
        }
        return null;
    }

    private void openWindowWebEdit(String url){
        ClientScriptOperation scriptOperation = context.requireSpecialist(OperationAgent.TYPE).getOperation(ClientScriptOperation.TYPE);
        String script = "window.open('"+url + "','_blank');";
        scriptOperation.perform(script, false);
    }

}