Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sajid Riaz 142 posts 165 karma points
    Jun 21, 2010 @ 21:28
    Sajid Riaz
    0

    Random Banner Images

    Hi Guys,

    I'm using the xslt below to get random images from a sub folder named bannerImages on my dev machine.  But at times it seems to bring up the open with dialogue (i.e. browser does not know the extension and what to open it with).  Sometimes it just shows the HTML in the browser window.

     

    The idea of the xslt is to bring back a random image for the banner.  I created a subfolder in the media folder named BannerImages.  On my master page I added a mediapicker property name bannerImages which was linked to the BannerImages folder.  Please advise me if the xslt is right or a better way of doing what i'm trying to do:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    <xsl:stylesheet
        version="1.0"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        xmlns:myFuncs="urn:my-scripts"
        exclude-result-prefixes="msxml myFuncs umbraco.library">


    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:variable name="imageRoot"     select="$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = 'bannerImages']"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript">
    <![CDATA[

    var myArray = new Array();

    function random(){    
        var index = Math.floor(Math.random() * myArray.length);   
        return myArray [index];
    }

    function addtoArray(item){
        myArray.push(item);
    }

    ]]>

    </msxsl:script>

    <xsl:template match="/">


    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node/data[@alias='umbracoFile']"/>
    </xsl:call-template>



    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:random()"/>
    <xsl:text>&amp;width=200</xsl:text>
    </xsl:attribute>
    </img>

    <br />
     <xsl:value-of select="myFuncs:random()"/>

    </xsl:template>

    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">
            <xsl:value-of select="myFuncs:addtoArray(string(./text()))"/>
        </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

     

     

     

  • Tommy Poulsen 514 posts 708 karma points
    Jun 21, 2010 @ 22:17
    Tommy Poulsen
    0

    Hi Sajid, if you skip rendering the images but just displays the random urls, do they look correct ?

    >Tommy

     

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 21, 2010 @ 23:10
    Stefan Kip
    0

    Sounds like a browser/OS problem instead of XSLT problem...

    Ps. you can change this:

    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:random()"/>
    <xsl:text>&amp;width=200</xsl:text>
    </xsl:attribute>
    </img>

    Into this as far as I know:

    <img src="/umbraco/imagegen.ashx?image={myFuncs:random()}&amp;width=200" />

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 10:29
    Sajid Riaz
    0

    Kipusoep thanks for the shorthand way!

    Tommy, yes the urls are comming back fine.  Also the page seems to render fine at times.

    and then sometimes when I click on a page link the browser takes sometime to think about it and shows the dialogue asking me what application I wish to open the aspx page with i.e. notepad, word pad etc etc. and sometimes I get the html viewable in the browser with a message at the top of the browser saying "object not found" 

    I thought it could've been the javascript but It seems ok. 

    If anyone has the time to test it I did it like this.

     

    I created a subfolder in media section, under media folder, named Banner Images.

    on my master page i added a mediapicker property named BannerImages, with an alias of bannerImages.

    I then set the BannerImages property to point at the BannerImages folder in the media section.

    The xslt was created as above and the corresponding macro inserted on the master page.

     

    thanks any help was be much approciated!

    Sajid 

     

     

     

     

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 12:22
    Tommy Poulsen
    0

    Is this only a problem with this page, or is it a general problem with your site?

    >Tommy

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 12:25
    Sajid Riaz
    0

    Hi Tommy,

     

    Just when I have the randomBanner xslt.  The strange thing is that it works ok most of the time.  But when I start clicking between links the open dialogue just pops up intermittantly.  I originally thought the problem was with the javascript but that seems fine.

    thanks

    Sajid

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 12:27
    Sajid Riaz
    0

    Tommy, the urls look something like: /media/19/xyz.jpg, where 19 I think is the id of the subfolder.

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 12:37
    Tommy Poulsen
    0

    Weird. And when you see the problem, is it then reproducable (i.e. clicking the link several tims will result in the same behavior) ?

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 12:43
    Tommy Poulsen
    0

    I would suspect the scavascript also - have you tried stepping through using Firebug?

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 12:45
    Tommy Poulsen
    0

    ... or make the random() function return a hardcoded image everytime.

    And maybe try to dump the content of your array.

     

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 13:23
    Sajid Riaz
    0

    Ok Not used Firebug for that.

    Once the pop up appears it then appears almost again, but takes several clicks to open up.

    Tommy your last post about returning the hardcoded image and the then dump the content of the array.

    Can you explain how I will do this?

    thanks again for your time

    Sajid

     

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 13:49
    Tommy Poulsen
    0

    Hi Sajid, I'll be happy to help you out - but I need to put it on hold for couple of hours. I will revert tonight.

    For returning a hardcoded image you have to modify random() function to something like this

    function random(){    
        return 'myhardcodedimageurl.jpg';
    }

    >Tommy

     

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 17:36
    Tommy Poulsen
    0

    Hi Sajid, you source code is working perfectly it my site - no problems, so I wonder if all the entries in your "bannerImages" are valid images?

    >Tommy

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 17:49
    Sajid Riaz
    0

    Tommy,  I placed the banner on my master page.

    and what I was doing was moving betwenn pages rapidly, just testing the speed of page load.

    did you try this on yr site?

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 17:53
    Tommy Poulsen
    0

    I tried changing pages quickly - no problem.

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 17:56
    Sajid Riaz
    0

    Ok Tommy thanks for that.  Much Appreciated.

     

     

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 17:59
    Sajid Riaz
    0

    Tommy, is your site live or on local host? 

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 18:00
    Sajid Riaz
    0

    I had 4 banner images all of type jpg.

  • Tommy Poulsen 514 posts 708 karma points
    Jun 22, 2010 @ 19:31
    Tommy Poulsen
    0

    it's on localhost, and I also had 4 banners of type jpg.

  • Sajid Riaz 142 posts 165 karma points
    Jun 22, 2010 @ 22:26
    Sajid Riaz
    0

    ok Tommy thanks for checking this out for me.

    Sajid

  • Sajid Riaz 142 posts 165 karma points
    Jun 24, 2010 @ 14:22
    Sajid Riaz
    0

    Incase any body gets similar problem.  The problem was with the jpg.

    I'm not sure if the size of the image was the problem.  But when i deleted an image everything was ok.

Please Sign in or register to post replies

Write your reply to:

Draft