[SalesForce] How to get parent window from a child page using JScript from SFDC Home Page Component VF Page</h1> </header><!-- .entry-header --> <div class="entry-content"> <p>I have a simple Salesforce.com Home Page component that has an iFrame to display my custom visual force page that has only one button on it.<br /> I am trying to find out how to display Parent Window (the main SFDC window) page title using javascript on a Button Click event. </p> <p>alert (Document.referrer) gives me the url for the page. However, I am struggling to get the page title that is displayed in the browser window as well. </p> <p>The screen show of what I am trying to achieve is as follows:</p> <p><img src="https://i.stack.imgur.com/8FqkR.png" alt="enter image description here"/></p> <p>Here is the sample code for Home Page Component</p> <pre><code><iframe src="/apex/Parent_Title" style="min-height:320px;max-height:400px;" id="myFrame" ="" frameborder="0" scrolling="no" width="200px"></iframe> </code></pre> <p>The custom VF Sample Code is as follow:</p> <pre><code> <apex:page showHeader="false" sidebar="false"> <script type="text/javascript"> function Show(){ var winTitle; // winTitle = <need to get parent window title here> ; alert('parent Window Title ='+ winTitle); } </script> <h1>My Test Page</h1> <br /> <input type="button" name="btn" onclick="Show()" value="Click Me" /> <!-- End Default Content REMOVE THIS --> </apex:page> </code></pre> <p>any javascript gurus in Salesforce world to help out here?</p> <ul> <li>Kashi</li> </ul> </div><!-- .entry-content --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8728145350222960" data-ad-slot="1638840875" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div id="comments" class="comments-area"> <div class="row"> <div class="col-12"> <div class="mt-3 border-bottom border-success"> <h4 class="text-success"><i class='fa fa-check-circle text-success mr-3'></i><span>Best Answer</span></h4> </div> <div class='bg-transparent mb-3'> <p>You could turn the problem around and pass the required information into the iFrame via the src queryString. </p> <p>E.g. My Javascript is a bit rough below and would probably be easier with some jQuery. Hopefully you get the idea.</p> <pre><code><iframe style="min-height:320px;max-height:400px;" id="myFrame" frameborder="0" scrolling="no" width="200px"></iframe> <script type="text/javascript"> document.getElementById('myFrame').src = '/apex/Parent_Title?Title=' + document.Title; </script> </code></pre> <p>Then in Parent_Title (method from <a href="https://stackoverflow.com/a/901144/54026">How can I get query string values in JavaScript?</a>):</p> <pre><code><script type="text/javascript"> function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } var winTitle = getParameterByName('Title'); </script> </code></pre> <p>It might be nicer to use Visualforce to inject the query string value into the Javascript. </p> <p>Of course, there is a <strong>BIG</strong> downside to this approach. It will open you up to JavaScript injection via the iframe query string parameter. At the very minimum you will need to sanitise the value coming off the query string. See <a href="https://help.salesforce.com/HTViewHelpDoc?id=review_and_certification.htm&language=en_US#XSS" rel="nofollow noreferrer">Cross-Site Scripting (XSS)</a> for a descirption of the issue.</p> </div> </div> <div class="col-4"></div> </div> </div><!-- #comments --> <div id="related-embeded" class="related-embeded-area"><div class="row"><div class="col-12"><div class="mt-3 border-bottom border-success"><h4 class="text-info"><i class='fa fa-check-circle text-info mr-3'></i><span>Related Solutions</span></h4></div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://life-improver.com/salesforce/salesforce-how-to-embed-standard-page-in-vf-page/'>[SalesForce] How to embed standard page in vf page</a></h5></div><div class='bg-transparent mb-3'> <p>You can try a parameter that sf uses but claims is not officially supported. Your iframe src add as follows: /006/o?isdtp=vw. Good luck.</p> <p>UPDATE: I tried creating a simple page to do this, and tried the "lt" parameter (the only other one I know about).</p> <pre><code><apex:page > <apex:sectionHeader title="Test Opportunities"/> <apex:pageblock > <apex:iframe src="/006/o?isdtp=lt"/> </apex:pageblock> </apex:page> </code></pre> <p>For me, the links are not underlined, but they are "click-able". </p> </div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://life-improver.com/salesforce/salesforce-saving-jquery-toggle-state-on-home-page-component/'>[SalesForce] Saving JQuery Toggle State on Home Page Component</a></h5></div><div class='bg-transparent mb-3'> <p>Cookie seems to be right approach, here is what I could quickly draft and test in a HTML component. </p> <pre><code><a onclick="setColor('red')" style="color:red;"> Give me Red ! </a> <br/> <a onclick="setColor('pink')" style="color:pink;"> Give me Pink ! </a> <script> var cookieName = 'colorSelection'; var cookieVal ; var allCookies = document.cookie.split(';'); for ( var idx = 0; idx < allCookies.length; idx++ ) { if (allCookies[idx].trim().indexOf(cookieName) == 0) { cookieVal = allCookies[idx].trim().split('=')[1]; break; } } if (cookieVal) { alert('Your last selected color was : ' + cookieVal); } function setColor(valColor) { document.cookie=cookieName + "=" + valColor; alert('color is ' + valColor + ' now'); } </script> </code></pre> <p>Above code sets a cookie via home page component, and reads it back on reload. One can change cookie value from red to pink or vice versa, via hyperlinks. </p> <p>You don't need jquery as such for manipulating cookies via Javascript. You might want to add "expiries" attribute to cookie, in case your values are time bound. </p> </div></div></div></div> </div> <div class="col-12 col-md-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8728145350222960" data-ad-slot="4237084052" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class='mt-3 ml-4 border-bottom border-success'><h6><span>Related Topic</span></h6></div><ul class='list-group list-group-flush'><li class="list-group-item"><a href='https://life-improver.com/salesforce/salesforce-javascript-pop-up-window-cross-origin-frame-issue/'>[SalesForce] JavaScript pop up window (cross-origin frame issue)</a></li><li class="list-group-item"><a href='https://life-improver.com/salesforce/salesforce-using-window-open-to-a-visual-force-page-in-a-new-window-strips-parameters-from-old-page/'>[SalesForce] using window.open to a visual force page in a new window strips parameters from old page</a></li></ul> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8728145350222960" data-ad-slot="8970621700" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <footer class="entry-footer"> </footer><!-- .entry-footer --> </article><!-- #post-293151 --> </div><!-- #main-col --> </div><!-- #main-row --> </div><!-- #main-container --> </main><!-- #main --> </div><!-- #primary --> </div><!-- #content --> <footer id="colophon" class="site-footer"> <div class="site-info"> </div><!-- .site-info --> </footer><!-- #colophon --> </div><!-- #page --> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/esm/popper.min.js?ver=1.14.7' id='popper-js'></script> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js?ver=4.3.1' id='bootstrap4-js'></script> </body> </html> <!-- Page supported by LiteSpeed Cache 5.0.1 on 2024-05-19 03:10:32 -->