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.
I am trying to find out how to display Parent Window (the main SFDC window) page title using javascript on a Button Click event.
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.
The screen show of what I am trying to achieve is as follows:
Here is the sample code for Home Page Component
<iframe src="/apex/Parent_Title" style="min-height:320px;max-height:400px;" id="myFrame" ="" frameborder="0" scrolling="no" width="200px"></iframe>
The custom VF Sample Code is as follow:
<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>
any javascript gurus in Salesforce world to help out here?
- Kashi
Best Answer
You could turn the problem around and pass the required information into the iFrame via the src queryString.
E.g. My Javascript is a bit rough below and would probably be easier with some jQuery. Hopefully you get the idea.
Then in Parent_Title (method from How can I get query string values in JavaScript?):
It might be nicer to use Visualforce to inject the query string value into the Javascript.
Of course, there is a BIG 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 Cross-Site Scripting (XSS) for a descirption of the issue.