daktau
daktau

Reputation: 643

How to get the anchor property of a URL of an iframe using javascript/jquery?

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

Answers (2)

Chase
Chase

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]);

DEMO

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]);

DEMO

Or, as @Pushpesh has recently noted in the comments section, a more condensed version would be:

$('#theIFrame').attr('src').split('#')[1];

Upvotes: 3

GautamD31
GautamD31

Reputation: 28763

Try with

$('#theIFrame').attr('src').text()

or try

$('#theIFrame').attr('href').split('#')[1]

Upvotes: 1

Related Questions