• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

How to capture the url of an image inside an iframe ?

Dear all,

My code is like this

<html>
<body>

<iframe src="http://www.test.com/test.asp?id=123"></iframe>

</body>
</html>

the resulting iframe will be a jpg and I need to capture its url, which will be something like http://www.test.com/output_123.jpg .

can I use Javascript to capture it ?  if yes, how ?

Thanks for helping.
Cristy


0
cristy56
Asked:
cristy56
  • 11
  • 8
  • 4
1 Solution
 
lil_puffballCommented:
Not sure I fully understand, but try this:

<iframe src="http://www.test.com/test.asp?id=123" id=ifr></iframe>
<button onclick="alert(document.getElementById('ifr').src);">Get URL</button>
0
 
cristy56Author Commented:
Hi lil_puffball,

Thanks for your reply, but it is not exactly like this.

I try your way and then the src will return to be "http://www.test.com/test.asp?id=123" , but what I want is to get the output of "http://www.test.com/test.asp?id=123" , which will be a jpg call http://www.test.com/output_123.jpg, I want to capture the jpg url instead of the iframe source.

I have tried
<button onclick="alert(document.getElementById('ifr.img').src);">Get URL</button>
but it doesn't seem to have this property ??

Cristy
0
 
lil_puffballCommented:
What is the html of "http://www.test.com/test.asp?id=123"??? How does it "call" the jpg?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lil_puffballCommented:
Do you mean the page writes something like "<img src=http://www.test.com/output_123.jpg>" to the iframe?
If so, maybe this will work: (it will only work if there is nothing between "img" and "src", if there are other properties, make sure to include them)

<script>
function getURL(txt){
var spos=txt.toLowerCase().indexOf("<img src=")+"<img src=".length;
var epos=txt.toLowerCase().indexOf(" ",pos);
return txt.substring(spos,epos);
}
</script>

<iframe src="http://www.test.com/test.asp?id=123" id=ifr></iframe>
<button onclick="alert(getURL(document.getElementById('ifr').innerHTML));">Get URL</button>
0
 
cristy56Author Commented:
the test.asp is just a sample, the logic is it gets some parameter and then returns a jpg to the resulting html.
Because I am not the one writing the test.asp part, I have no code on hand to show you, sorry about this.

so the resulting html will only have

<html>
<body>
<img src="test_123.jpg">
</body>
</html>

I have tried
<button onclick="alert(document.getElementById('ifr').images[0].src);">Get URL</button>
but is still not working, do u know why ?

Actually there is only one jpg and nothing else in the output of the test.asp



0
 
lil_puffballCommented:
Never mind, that didn't work.

If you can give the image a name, like so:

<img src=http://www.test.com/output_123.jpg name=img>

then the following code will work:

<iframe src="test6.htm" id=ifr></iframe>
<button onclick="alert(window.frames.ifr.document.images('img').src);">Get URL</button>
0
 
lil_puffballCommented:
Well, this worked for me:

<iframe src="test6.htm" id=ifr></iframe>
<button onclick="alert(window.frames.ifr.document.images[0].src);">Get URL</button>
0
 
lil_puffballCommented:
Oh, I see now, you were missing the .document in your code...
0
 
cristy56Author Commented:
error : pos is not yet defined ??
0
 
lil_puffballCommented:
Yeah, my first code was messed up, try my last one. :)
0
 
cristy56Author Commented:
but I cannot "name" to the img in the putput html :( , so what should I do ?
0
 
cristy56Author Commented:
sorry for all the inconvenience caused
0
 
lil_puffballCommented:
I meant my last LAST one (I had 3 codes...):

<iframe src="test6.htm" id=ifr></iframe>
<button onclick="alert(window.frames.ifr.document.images[0].src);">Get URL</button>
0
 
cristy56Author Commented:
still not working :(
it said :Access denied ??
0
 
cristy56Author Commented:
Maybe I should clarify my question a bit.

First of all, I got an html, inside there is an iframe,
the src for this iframe is some script (e.g test.asp?id=123) that will return a jpg in the output.

however, the test.asp?id=123 is not hosted in my server, but it is located in other web server.

What I need to do now is, I need to capture the output jpg of the test.asp?id=123, the output jpg actually differs everytime.

I have tried all the above method. And I found the following problem.
if the iframe src is just a normal htm, everything works fine. But if it is a asp or some other scriptlets, that I would not be able to capture the image url with the above method and the error is access denied.

is that any good suggestion now ? thanks lil_puffball for helping :)
0
 
cristy56Author Commented:
P.s. To be precise,  I need to capture the url of the output jpg each time
0
 
thirdCommented:
for security reasons, you are not allowed to do that.

workaround is to rename your html file to hta (i.e. html application) but you can only run this on your local pc not on the web. tell me if this is what you want.


grabber.hta
---------------
<html>
<body>
<form name="test1">
<input type="text" name="text1" value="test1">
</form>
<iframe src="http://www.google.com" name="myframe"></iframe>
<a href="#" onclick="alert(document.myframe.document.images[0].src);">click</a>
</body>
</html>
0
 
thirdCommented:
opps, remove the form tags. those are not necessary. 'twas from a previous test. ;-)
0
 
cristy56Author Commented:
I have the htm running on the web and not my local PC.

Why is this security reason again ?? I can also get the image name if I right click on the jpg inside the iframe. Is there really something to deal with secuirty ?
0
 
cristy56Author Commented:
Hi all,

test.hta
============
<html>
<body>
<iframe src="http://www.test.com/servlet?id=12"  name="myframe"></iframe>
<a href="#" onclick="alert(document.myframe.document.images[0].src);">click</a>
</body>
</html>
=============

the above output is exactly what I want, but how can I turn it to an htm or someother format that can be run on web server ?
0
 
thirdCommented:
as i have mentioned earlier, you cannot do that from a web server.
0
 
cristy56Author Commented:
then is there any other way round, or using other method ?
0
 
thirdCommented:
IMO, 'no' is still an answer so why delete it?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 11
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now