function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swapnil Patne 20Swapnil Patne 20 

A command button on a Visualforce page and upon click I need to open a URL in an iFrame

Hi all,

I have a command button on a Visualforce page and upon click the URL should open in an iFrame i.e. a web page inside an iFrame 

Idea is to display 'Button' on VF page and below that iFrame (both in VF page) and upon clicking the button it opens the URL/web page in an iFrame..

My development expertise are very limited and I've been researching and learning how to do things, but here is my code I am working.. please can you help me resolve the issue or with the correct code..?

<apex:page sidebar="False">

<apex:form >
  <apex:commandButton onclick="toGoogle();" value="Google"/>
 </apex:form>
 
  <script>
   function toGoogle() {

        redirectTo('http://www.google.com');
    }

   
    function redirectTo(target) {

        if (inIframe()) {
           // step out of an iframe (if opened via iframe)
            window.top.location.href = target;
            return false;
        } else {
          window.location.href = target;
        }
    }

    function inIframe() {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }
  </script> -->

</apex:page>

Thanks in advance.

Swapnil