Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Fix setting/getting document title when within a crossdomain iframe #507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/view/pane/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
*/
Candy.View.Pane = (function(self) {

// Partial implementation based on
// http://stackoverflow.com/questions/21751377/foolproof-way-to-detect-if-this-page-is-inside-a-cross-domain-iframe
function isCrossDomain() {
return window.self !== window.top;
}

/** Class: Candy.View.Pane.Window
* Window related view updates
*/
Expand All @@ -27,7 +33,7 @@ Candy.View.Pane = (function(self) {
/** PrivateVariable: _plainTitle
* Document title
*/
_plainTitle: window.top.document.title,
_plainTitle: isCrossDomain() ? document.title : window.top.document.title,
/** PrivateVariable: _unreadMessagesCount
* Unread messages count
*/
Expand All @@ -38,6 +44,20 @@ Candy.View.Pane = (function(self) {
*/
autoscroll: true,

/** Function: setDocumentTitle
* Sets the document title, if possible of the topmost window (in case it's in a frame).
*
* Parameters:
* (String) title - Set new title
*/
setDocumentTitle: function(title) {
var win = window;
if (!isCrossDomain()) {
win = window.top;
}
win.document.title = title;
},

/** Function: hasFocus
* Checks if window has focus
*
Expand Down Expand Up @@ -75,7 +95,7 @@ Candy.View.Pane = (function(self) {
*/
clearUnreadMessages: function() {
self.Window._unreadMessagesCount = 0;
window.top.document.title = self.Window._plainTitle;
self.Window.setDocumentTitle(self.Window._plainTitle);
},

/** Function: renderUnreadMessages
Expand All @@ -85,7 +105,7 @@ Candy.View.Pane = (function(self) {
* (Integer) count - Number of unread messages to show in window title
*/
renderUnreadMessages: function(count) {
window.top.document.title = Candy.View.Template.Window.unreadmessages.replace('{{count}}', count).replace('{{title}}', self.Window._plainTitle);
self.Window.setDocumentTitle(Candy.View.Template.Window.unreadmessages.replace('{{count}}', count).replace('{{title}}', self.Window._plainTitle));
},

/** Function: onFocus
Expand Down