Leon
Leon

Reputation: 1292

Get iframe src param from inside the iframe itself

i have an iframe and need to capture the src attribute inside a javascript variable. This is how my iframe looks like

<iframe id="iframe_module_46081" name="iframe_module_46081" src="//myurl.com/tabs/495/campaigns/0ed3ffa8-5050-4a18-a5e3-f348d12a1304?signature=6aa3005368f163539976460246f2f0839c0fb3e3&amp;id=678326">
<script>$(document).ready(function(){
  var name = window.name;
  var src = ????;
})
</script>
</iframe>

I am able to get the name through window.name but not the src attribute. Any idea?

Upvotes: 13

Views: 19062

Answers (4)

Rantiev
Rantiev

Reputation: 2232

As for me for now I ended up with saving initial iframe location.href to window object. (window.PROJECT_NAME_FIRST_URL_LOADED = window.location.href) Somewhere in application bootstrapping before application can change it's URL.

The other place where we could take it is history API, but it has no info about initial URL before you go back to initial state I believe.

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32286

If allowed by cross-domain checks, try window.frameElement.src. Otherwise, you are out of luck and left alone with location.href.

Upvotes: 23

JavaKungFu
JavaKungFu

Reputation: 1314

var src = top.document.getElementById("iframe_module_46081").src;

If you don't want to use jquery selector.

Upvotes: 1

SLaks
SLaks

Reputation: 887275

You're looking for document.location, just like any other document.
Each frame gets its own document.

Upvotes: 3

Related Questions