...

View Full Version : Best Way To Use Javascript To Find Absolute Path Of Img Src?



dealmaker
07-21-2005, 07:01 PM
Hi,
The src in img tag usually doesn't show the absolute path of an image, is there a javascript built-in function that can find that? If not, do you know of the best way to do that?

There are a lot of potential problems, like if the url of the webpage is www.abc.com/hello/image/, the src in img tag can be "../../image2.jpg". Or it can link to some other websites, etc.

Many thanks.

Johnny Lang
07-21-2005, 07:15 PM
My code was ignored, so I deleted it, which means I am removing it and withdrawing it from consideration not only in this thread, but in this forum as well. So, if anyone has made a copy of MY code, I respectfully request that you do not repost like someone did in a similar situation yesterday. I find it offensive and disrespectful. And if the author of a question fails or refuses to even acknowledge that I posted code, then it's MY choice if I want to delete MY code. MY CODE, MY POST, I'll delete it whenever I choose.

Thank you.

jscheuer1
07-21-2005, 07:16 PM
The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:

function getImgPath(img){
image1=new Image()
image1.src=img
return image1.src
}This function will accept as an argument an image, like:

getImgPath('some.gif')and return its full path. Try this to see how it works:

<img src="some.gif" onmouseover="alert(getImgPath(this.src))">

dealmaker
07-21-2005, 08:24 PM
But that src will still be whatever it is in the src in img tag, e.g. "../../some.gif". So what you are saying is to integrate your method with the one above you, right?


The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:

function getImgPath(img){
image1=new Image()
image1.src=img
return image1.src
}This function will accept as an argument an image, like:

getImgPath('some.gif')and return its full path. Try this to see how it works:

<img src="some.gif" onmouseover="alert(getImgPath(this.src))">

jscheuer1
07-21-2005, 09:31 PM
No, that person cross posted and then got ticked off that we didn't comment on his code right away. I never saw it. Just try this:

<html>
<head>
<title></title>
<script type="text/javascript">
function getImgPath(img){
image1=new Image()
image1.src=img
return image1.src
}
</script>
</head>
<body>
<img src="some.gif" onmouseover="alert(getImgPath(this.src))">
</body>
</html>View the page in your browser and see what happens when you mouse over the image.

dealmaker
07-21-2005, 10:47 PM
that's crazy, I never meant to annoy his post, I am just waiting for more advices and opinions. And obviously, you kindof referred to his post when you replied, so no one ignored his post.

Anyway, your code will return the src which is "some.gif", not the absolute path, like "www.abc.com/image/some.gif". Correct me if I am wrong.


No, that person cross posted and then got ticked off that we didn't comment on his code right away. I never saw it. Just try this:

<html>
<head>
<title></title>
<script type="text/javascript">
function getImgPath(img){
image1=new Image()
image1.src=img
return image1.src
}
</script>
</head>
<body>
<img src="some.gif" onmouseover="alert(getImgPath(this.src))">
</body>
</html>View the page in your browser and see what happens when you mouse over the image.

jscheuer1
07-21-2005, 11:25 PM
Did you try it out?

Willy Duitt
07-22-2005, 12:05 AM
My code was ignored, so I deleted it, which means I am removing it and withdrawing it from consideration not only in this thread, but in this forum as well. So, if anyone has made a copy of MY code, I respectfully request that you do not repost like someone did in a similar situation yesterday. I find it offensive and disrespectful. And if the author of a question fails or refuses to even acknowledge that I posted code, then I believe it's MY choice if I want to delete MY code. Thank you.

No, it's not your choice to ruin the integrity of a thread and return and delete your code which you have been doing for days now (this is not the first incident)... Part of these forums as a community is the archives which allows others to search for a solution... Removing your code/posts is less welcome than posting links to a site that may not be available in the future...

I've removed a few posts since I have been here... Either because they were inappropriate because they did not adequately answer the O/P's question or I made revisions to the original script and removed the previous script so that it did not confuse the issue... But removing your code (for days on end) is simply wrong...

.....Willy

jscheuer1
07-22-2005, 01:38 PM
I don't think Johnny Lang ruined anything in this particular case but, definitely overreacted, as we all have at times. He doesn't seem to understand that others may be writing and posting at the same time as he, thus making it appear that his post is not receiving immediate attention or worse, the wrong type of attention. Something none of us should really expect and/or worry too much about anyway. When I started my first reply with:

The only reliable cross browser method would be to assign the source to an image object . . .It might have appeared I was diminishing something he had posted. In truth, I never saw his original contribution to this thread. By the time I checked back, he had already gone off in a huff.

dealmaker
07-23-2005, 08:04 AM
Is there any reason you can't just do alert(this.src) in the html?

If not, how about this:

function getImgPath(img){

return img.src;
}


The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:

function getImgPath(img){
image1=new Image()
image1.src=img
return image1.src
}This function will accept as an argument an image, like:

getImgPath('some.gif')and return its full path. Try this to see how it works:

<img src="some.gif" onmouseover="alert(getImgPath(this.src))">

jscheuer1
07-23-2005, 12:41 PM
Now I can see you've tried the code. I don't know, seems to work in IE6 and FF but, some browsers might return the text of the src attribute when using this.src. The img.src function you wrote should be fine. My method is 100% reliable across all browsers that support document.images, if they don't do that, they don't display images anyway. So, I'd rewrite my function:

function getImgPath(img){
if (!document.images)
return;
image1=new Image()
image1.src=img
return image1.src
}In any event, I guess you have solved your problem, right?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum