Reputation: 643
I am trying to get the anchor part of a URL from an iFrame using javascript or jQuery.
eg.
<iframe id="theIFrame" src="location.html#anchorHere"></iframe>
if I use the src attribute I don't get the hash.
eg.
$('#theIFrame').attr('src')
will return 'location.html'
How do I get the hash?
thank you, George
Upvotes: 0
Views: 808
Reputation: 29549
To get everything after the #
mark, you can try the following:
var href = $("#theIFrame").attr("href");
var splitHref = href.split("#");
alert(splitHref[1]);
EDIT
OP was was changed from:
<iframe id="theIFrame" href="location.html#anchorHere"></iframe>
to:
<iframe id="theIFrame" src="location.html#anchorHere"></iframe>
so you would need to change the code to:
var href = $("#theIFrame").attr("src");
var splitHref = href.split("#");
alert(splitHref[1]);
Or, as @Pushpesh has recently noted in the comments section, a more condensed version would be:
$('#theIFrame').attr('src').split('#')[1];
Upvotes: 3
Reputation: 28763
Try with
$('#theIFrame').attr('src').text()
or try
$('#theIFrame').attr('href').split('#')[1]
Upvotes: 1