Getting the iframe's URL parameter from within the iframe - javascript

I have an iframe that is loaded on web page and I do not have permissions to access the parent page. I have access to the iframe's code. I would like to access one of the parameters of this iframe. What is the best way to achieve this?
I tried location.search but it does not give me all the parameters.
Any suggestions?
Cheers.

I managed to find the answer myself and we can retrieve the iframe's URL using the following code:
document.location.href
This seems to work fine without any issues.

Related

how to access the external website iframe having a title [duplicate]

I wonder if it is possible to get the page title of an url within IFRAME. I tried to use jQuery $('#iframe').attr('title') but that doesn't work. Please help. Thanks.
I think this is what you're looking for:
$(document).ready(function()
{
alert($('#iframe').contents().find("title").text());
});
That will return the page title that is located in the <title> tag of the iframe document that has the id "iframe".
I misread your question.
$('title', frames[frameName].document).text();
If what you are looking for is a way to get the URL of the iframe after the user has clicked on a link within that iframe, this is not possible in any modern browser that blocks cross-domain attempts.
Even though the iframe is part of the DOM and you can easily find the new iframe URL using apps like Firebug, Firefox will throw a XSS error on any attempts by js to directly pull that info.
But for the record, as it's already been said, the location within the DOM of the actual URL of the iframe content is (with a little help from jquery) : $("#id_of_iframe).contentDocument.location.href
I'm not totally sure if the above points straight to it with the above syntax, but that's the gist of it. The part that is a no-no is trying to go inside that contentDocument part.

Get content of an iframe after it loads with

I want to achieve the fallowing goal.I want by the click of a button to get the domain name entered in one input and load it into an iframe. What i have trouble with is after i load the specific site in the iframe how can i get the DOM of the loaded site in the iframe ? I need to save the exact path of an element of my choosing from the newly loaded site in the iframe by clicking on it. Any help would be much appreciated. Thank you.
You can access the <iframe>'s contents via the contentDocument property.
var iFrameContent = $('myIFrame')[0].contentDocument;
$(iFrameContent).find('.mySelector').text();
I should also point out accessing the <iframe>'s contents can only be done if the src of the <iframe> is from the same domain as your script. If the src of the <iframe> is from any other domain, the browser will deny access to the <iframe>contents.
This is a CORS consideration.
UPDATE:
To get around the CORS limitation, you'll have to write a server-side proxy for the URL that is the src of the <iframe> (and it could be pretty simple).
If I were to do something like this in ASP.Net (as an example), I would write a web service that:
takes the actual URL as a String parameter.
Uses the HTTPWebRequest object (or similar) to get the URL contents.
Return it back to the browser.
Then you could set your <iframe> src to "http://mysite.com/getURLService?url=www.lalala.com" and get the contents because the contents are now delivered by "http://mysite.com".
You use .contents()
like $("iframe").contents().find(); etc

Can a page loaded in an iframe find out the url of its parent? [duplicate]

I got a page that is displayed within an iframe.
I need to get the parent.location.url with js from that page (child page).
Both sites are in different domains.
I'm trying:
alert(parent.location.url)
But I get this error:
Permission denied for http://parentdomain to get property Location.URL from http://childdomain.
???? Is it possible?
try this:
var referrer = document.referrer;
alert(referrer);
What you're trying to do isn't possible per se. Unless you have control over the parent page's calling of the child page. If you do, then you can call the child page with a get statement in the URL of the parent page. So if you parent page is http://parentpage.com then here's how you should call the iFrame.
<iframe src='http://childpage.com/?parent=http://parentpage.com' />
Then all you have to do in the child page to get the reference is get the string with
window.location.search //not sure of browser compatibility with this
and extract the parent page's URL by grabbing it out of that string. I'd give more details, but I'm not even sure about the first question I had.
Have you tried something like:
alert(window.top.location.href);
You'll be able to find it like this, and all browsers should be able to support it:
query = window.parent.location.search.substring(1);
You shouldn't get permission errors with this, but I may be wrong.
It will only work if the parent page and the iframe are on the same domain. If they are not on the same domain you can try passing the location through the src, like Madison Williams suggested.
That is probably why you are getting "Permission denied..."
Use postMessage to pass data back and forth between the windows. It's a little klunky, but required when having windows communicate across origins.
Try:
var pathname = window.parent.location.href
alert(window.top.location.href);
This works fine. To get the source URL from child (iframe) to parent.

Javascript top.location.replace with and without parent iFrame

I am using a frames page, which contains the following Javascript line:
top.contentsFrame.location.replace("/main.jsp");
I want to run this page both inside an iFrame, as well as independenty.
In an iFrame it doesn't work, because the top attribute now returns the iFrame. So it can't find "contentsFrame" anymore.
What would be the easiest way to fix this?
Cheers!
you should use
parent.top.location.replace("/main.jsp");

Get title string from url within iframe using jQuery?

I wonder if it is possible to get the page title of an url within IFRAME. I tried to use jQuery $('#iframe').attr('title') but that doesn't work. Please help. Thanks.
I think this is what you're looking for:
$(document).ready(function()
{
alert($('#iframe').contents().find("title").text());
});
That will return the page title that is located in the <title> tag of the iframe document that has the id "iframe".
I misread your question.
$('title', frames[frameName].document).text();
If what you are looking for is a way to get the URL of the iframe after the user has clicked on a link within that iframe, this is not possible in any modern browser that blocks cross-domain attempts.
Even though the iframe is part of the DOM and you can easily find the new iframe URL using apps like Firebug, Firefox will throw a XSS error on any attempts by js to directly pull that info.
But for the record, as it's already been said, the location within the DOM of the actual URL of the iframe content is (with a little help from jquery) : $("#id_of_iframe).contentDocument.location.href
I'm not totally sure if the above points straight to it with the above syntax, but that's the gist of it. The part that is a no-no is trying to go inside that contentDocument part.

Categories

Resources