Skip to content

Commit 355d2e0

Browse files
committed
Ticket #96 - Make hash links scroll the iframe, not open a new browser window
1 parent a0b2eab commit 355d2e0

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

epiceditor/js/epiceditor.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,10 @@
922922
* @returns {object} EpicEditor will be returned
923923
*/
924924
EpicEditor.prototype.preview = function (theme) {
925-
var self = this;
926-
925+
var self = this
926+
, x
927+
, anchors;
928+
927929
theme = theme || self.settings.basePath + self.settings.theme.preview;
928930

929931
_replaceClass(self.getElement('wrapper'), 'epiceditor-edit-mode', 'epiceditor-preview-mode');
@@ -935,10 +937,21 @@
935937
else if (self.previewerIframeDocument.getElementById('theme').name !== theme) {
936938
self.previewerIframeDocument.getElementById('theme').href = theme;
937939
}
938-
940+
939941
// Add the generated HTML into the previewer
940942
self.previewer.innerHTML = self.exportFile(null, 'html');
941-
943+
944+
// Because we have a <base> tag so all links open in a new window we
945+
// need to prevent hash links from opening in a new window
946+
anchors = self.previewer.getElementsByTagName('a');
947+
for (x in anchors) {
948+
// If the link is a hash AND the links hostname is the same as the
949+
// current window's hostname (same page) then set the target to self
950+
if (anchors[x].hash && anchors[x].hostname == window.location.hostname) {
951+
anchors[x].target = '_self';
952+
}
953+
}
954+
942955
// Hide the editor and display the previewer
943956
if (!self.is('fullscreen')) {
944957
self.editorIframe.style.display = 'none';

0 commit comments

Comments
 (0)