Results 1 to 5 of 5

Thread: Swap image based on url variable

  1. #1
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swap image based on url variable

    hello all,

    I need to swap an image based on a url variable.


    *Nevermind, I figured it out.*
    Last edited by knight006; 11-19-2006 at 04:53 AM.

  2. #2
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can you or someone show how this is done?

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Let's call it swap_pic.htm:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function getQval(n, m) {
    /*my n=name, m=searchString(optional)*/
    if(!arguments[0]||typeof n!='string')
    return null;
    var r=new RegExp('[?&;]'+n+'=([^&;#]*)'), m=arguments[1]?m:location.search;
    return (m=r.exec(m))? unescape(m[1]) : null;
    }
    function swap_it(im){
    var s=['photo1.jpg', 'photo2.jpg', 'photo3.jpg', 'photo4.jpg', 'photo5.jpg'];
    var im=document.images[im];
    if (getQval('pic')&&s[getQval('pic')])
    im.src=s[getQval('pic')];
    }
    onload=function(){swap_it('my_image');};
    </script>
    </head>
    <body>
    <img id="my_image" src="photo1.jpg">
    </body>
    </html>
    Now you can have what ever images, and however many images you want in the red array. Arrays are numbered internally 0 to whatever. So, if you were to link to this page with - say:

    Code:
    <a href="swap_pic.htm?pic=2">Link</a>
    It will load the image from the 2 position in the array, which is photo3.jpg.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you sir. Though here is the situation. I should have be more specific the first time. I'm working with an iframe file and the JavaScript code needs be to place in the src file. The iframe has several different subdomains. The JavaScript should determine its parent subdomain then display a specific image.

    My limited knowledge wrote the below logic, but my research tells me that this is a security risk and generates a permission denied error. Is what I'm trying to do possible?

    var domain = parent.location.href;
    var subdomain1 = 'subdomain1.domain.com';
    var subdomain2 = 'subdomain2.domain.com';
    if(domain = subdomain1)
    {document.write("<a href=\"http://www.url.com\" target=\"_blank\"><img src=\'image.gif' width=\"100\" height=\"100\" border=\"0\"></a>");
    }
    else {
    if(domain = subdomain2)
    {document.write("<a href=\"http://www.url2.com\" target=\"_blank\"><img src=\'image2.gif' width=\"20\" height=\"20\" border=\"0\"></a>");}
    }

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That would be a security violation, but also an error. If there are more than one subdomains to check against, you would need some kind of server side code to deal with the situation. Even then, the configuration of the servers involved would need to be such as to allow it.

    Using javascript, the best I think that you can determine from the child page is whether or not the parent domain is the same as the child domain:

    Code:
    <script type="text/javascript">
    try {
    var s=parent.location.hostname? parent.location.hostname : 'offsite';
    }
    catch(e){
    var s='offsite';
    }
    alert(s)
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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