Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function () {
function _addClick(browser, textboxID) {
return function (e) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
function loaded () {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var urlbarURL = browser.getUserData('urlbarURL');
var url = urlbarURL || browser.contentDocument.defaultView.location.href;
if (urlbarURL) {
browser.setUserData('urlbarURL', null, null);
}
if (browser.getUserData('backNow')) { // This load was a result of a back event, so don't increment as we normally do
browser.setUserData('backNow', false, null);
return;
}
if (browser.getUserData('forwardNow')) { // This load was a result of a forward event, so don't alter the array as we normally do
browser.setUserData('forwardNow', false, null);
return;
}
var history = browser.getUserData('history');
if (history == null) {
history = [browser.src];
browser.setUserData('history', history, null);
browser.setUserData('historyPointer', 0, null);
}
var historyPointer = browser.getUserData('historyPointer');
history.splice(historyPointer+1, history.length, url);
document.getElementById(textboxID).value = url;
var newPointer = historyPointer+1;
browser.setUserData('historyPointer', newPointer, null);
browser.setUserData('history', history, null);
}
catch(err) {
alert(err);
}
}
browser.addEventListener('load', loaded, false);
browser.setUserData('hasListener', true, null);
}
catch(err) {
alert(err);
}
};
}
function goURL (iframeID, textboxID) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var browser = document.getElementById(iframeID);
var textbox = document.getElementById(textboxID);
if (!browser.getUserData('hasListener')) {
_addClick(browser, textboxID)();
}
browser.setUserData('urlbarURL', textbox.value, null);
browser.contentDocument.defaultView.location.href = textbox.value;
}
catch(e) {
alert(e);
}
}
function back (iframeID, textboxID) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var browser = document.getElementById(iframeID);
var history = browser.getUserData('history');
if (!history) {
return;
}
var historyPointer = browser.getUserData('historyPointer');
if (historyPointer >= 1) {
var newPointer = historyPointer-1;
browser.setUserData('historyPointer', newPointer, null);
browser.setUserData('backNow', true, null);
browser.contentDocument.defaultView.location.href = history[newPointer];
document.getElementById(textboxID).value = history[newPointer];
}
}
catch(e) {
alert(e);
}
}
function forward (iframeID, textboxID) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var browser = document.getElementById(iframeID);
var history = browser.getUserData('history');
if (!history) {
return;
}
var historyPointer = browser.getUserData('historyPointer');
if (historyPointer < history.length-1) {
var newPointer = historyPointer+1;
browser.setUserData('historyPointer', newPointer, null);
browser.setUserData('forwardNow', true, null);
browser.contentDocument.defaultView.location.href = history[newPointer];
document.getElementById(textboxID).value = history[newPointer];
}
}
window.addEventListener('load', function () {
var frameBrowsers = document.getElementsByClassName('frameBrowser');
for (var i=0, fbl=frameBrowsers.length; i < fbl; i++) {
var browser = frameBrowsers[i];
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var number = browser.id.replace(/^iframeBrowser/, ''); // (i+1) would works, but only for sequential entries starting from 1, so to be safe, we depend on the ID which contains the number
_addClick(browser, 'urlBar'+number)();
//browser.contentDocument.addEventListener('click', _addClick(browser, 'urlBar'+number, true), true); // Shouldn't it be true????
document.getElementById('urlBar'+number).value = browser.src;
}
catch(e) {
alert(e);
}
}
}, false);
var JSBrowser = {};
JSBrowser.goURL = goURL;
JSBrowser.back = back;
JSBrowser.forward = forward;
JSBrowser._addClick = _addClick;
this.JSBrowser = JSBrowser;
}());
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Independent JavaScript Browsers</title>
<script type="text/javascript" src="testIframeNew1.js"></script>
<link rel="stylesheet" type="text/css" href="testIframeNew1.css" />
</head>
<body>
<div class="set">
<div><a id="back1" href="javascript:void(0);" onclick="JSBrowser.back('iframeBrowser1', 'urlBar1');">&lt;</a> &#160; <a href="javascript:void(0);" id="forward1" onclick="JSBrowser.forward('iframeBrowser1', 'urlBar1');">&gt;</a> &#160; <input id="urlBar1" type="text" size="50" /> <input type="button" onclick="JSBrowser.goURL('iframeBrowser1', 'urlBar1');" value="Go!" /></div>
<iframe id="iframeBrowser1" class="frameBrowser" src="test3Iframe.html"></iframe>
</div>
<div class="set">
<div><a id="back2" href="javascript:void(0);" onclick="JSBrowser.back('iframeBrowser2', 'urlBar2');">&lt;</a> &#160; <a href="javascript:void(0);" id="forward2" onclick="JSBrowser.forward('iframeBrowser2', 'urlBar2');">&gt;</a> &#160; <input id="urlBar2" type="text" size="50" /> <input type="button" onclick="JSBrowser.goURL('iframeBrowser2', 'urlBar2');" value="Go!" /></div>
<iframe id="iframeBrowser2" class="frameBrowser" src="test2Iframe.html"></iframe>
</div>
</body>
</html>
<a href="http://www.yahoo.com">Yahoo</a>
<a href="http://www.google.com">Google</a>
.set {float:left;width:50%}
.frameBrowser {width:450px;height:500px;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.