Skip to content

Instantly share code, notes, and snippets.

@mmanela
Created July 30, 2012 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmanela/3207867 to your computer and use it in GitHub Desktop.
Save mmanela/3207867 to your computer and use it in GitHub Desktop.
Linking between anchors in an IFrame (Chrome and Firefox 11+)
$(function() {
var iframeOffset = $("#ID_OF_MY_IFRAME", window.parent.document).offset();
$("a").each(function () {
var link = $(this);
var href = link.attr("href");
if (href && href[0] == "#") {
var name = href.substring(1);
$(this).click(function () {
var nameElement = $("[name='" + name + "']");
var idElement = $("#" + name);
var element = null;
if (nameElement.length > 0) {
element = nameElement;
} else if (idElement.length > 0) {
element = idElement;
}
if (element) {
var offset = element.offset();
window.parent.scrollTo(offset.left, offset.top + iframeOffset.top);
}
return false;
});
}
});
});
Copy link

ghost commented Aug 30, 2013

Great script... just change the name of the #frameid and stick it in the head of your document! This script was a lifesaver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment