Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Best Way To Use Javascript To Find Absolute Path Of Img Src?

    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.

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Johnny Lang; 07-21-2005 at 11:16 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:
    Code:
    function getImgPath(img){
    image1=new Image()
    image1.src=img
    return image1.src
    }
    This function will accept as an argument an image, like:
    Code:
    getImgPath('some.gif')
    and return its full path. Try this to see how it works:
    Code:
    <img src="some.gif" onmouseover="alert(getImgPath(this.src))">

  • #4
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Quote Originally Posted by jscheuer1
    The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:
    Code:
    function getImgPath(img){
    image1=new Image()
    image1.src=img
    return image1.src
    }
    This function will accept as an argument an image, like:
    Code:
    getImgPath('some.gif')
    and return its full path. Try this to see how it works:
    Code:
    <img src="some.gif" onmouseover="alert(getImgPath(this.src))">

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <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.

  • #6
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Quote Originally Posted by jscheuer1
    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:
    Code:
    <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.

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did you try it out?

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Johnny Lang
    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

  • #9
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Quote Originally Posted by me
    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.

  • #10
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }

    Quote Originally Posted by jscheuer1
    The only reliable cross browser method would be to assign the source to an image object and then retrieve its source ex:
    Code:
    function getImgPath(img){
    image1=new Image()
    image1.src=img
    return image1.src
    }
    This function will accept as an argument an image, like:
    Code:
    getImgPath('some.gif')
    and return its full path. Try this to see how it works:
    Code:
    <img src="some.gif" onmouseover="alert(getImgPath(this.src))">

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •