Created
July 30, 2012 15:36
-
-
Save mmanela/3207867 to your computer and use it in GitHub Desktop.
Linking between anchors in an IFrame (Chrome and Firefox 11+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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; | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script... just change the name of the #frameid and stick it in the head of your document! This script was a lifesaver!