Javascript help needed

Discussion in 'HTML & Website Design' started by ChannerUK, Sep 25, 2007.

  1. #1
    Hi guys. I'm putting a gallery script on my website and I keep getting the followin error 'href' is null or not an object on the following line var popwin=open(imgsrc.href, "popwin", popupsetting[1]) in the code below.

    <script type="text/javascript">
    
    
    
    var dimension="5x4" 
    var imagepath="images/gallery/" 
    var href_target="new" 
    
    var popupsetting=[1, "width=500px, height=400px, scrollbars, resizable"]
    
    var descriptionprefix=[0]
    
    var gsortorder="desc"
    
    var targetlinkdir="images/gallery/"
    
    
    function sortbydate(a, b){ 
    if (gsortorder=="asc") 
    return new Date(a[1])-new Date(b[1])
    else if (gsortorder=="desc") 
    return new Date(b[1])-new Date(a[1])
    }
    
    if (gsortorder=="asc" || gsortorder=="desc")
    galleryarray.sort(sortbydate)
    
    var totalslots=dimension.split("x")[0]*dimension.split("x")[1]
    
    function buildimage(i){
    var imagecompletepath=(targetlinkdir!="")? targetlinkdir+galleryarray[i][0] : imagepath+galleryarray[i][0]
    var tempcontainer='<a href="'+imagecompletepath+'" target="'+href_target+'" onClick="return popuplinkfunc(this)">'
    tempcontainer+='<img src="'+imagepath+galleryarray[i][0]+'" title="'+galleryarray[i][0]+' ['+galleryarray[i][1]+']" />'
    tempcontainer+='</a><br />'
    tempcontainer+=(descriptionprefix[0]==1)? descriptionprefix[1]+(i+1) : ""
    return tempcontainer
    }
    
    function jumptopage(p){
    var startpoint=(p-1)*totalslots
    var y=1;
    for (i=0; i<totalslots; i++){
    document.getElementById("slide"+i).innerHTML=(typeof galleryarray[startpoint+i]!="undefined")? buildimage(startpoint+i) : ""
    }
    while(document.getElementById("navlink"+y)!=null){
    document.getElementById("navlink"+y).className=""
    y++
    }
    document.getElementById("navlink"+p).className="current"
    }
    
    var curimage=0
    for (y=0; y<dimension.split("x")[1]; y++){
    for (x=0; x<dimension.split("x")[0]; x++){
    if (curimage<galleryarray.length)
    document.write('<div id="slide'+curimage+'" class="slideshow">'+buildimage(curimage)+'</div>')
    curimage++
    }
    document.write('<br style="clear: left" />')
    }
    
    function popuplinkfunc(imgsrc){
    if (popupsetting[0]==1){
    var popwin=open(imgsrc.href, "popwin", popupsetting[1])
    popwin.focus()
    return false
    }
    else
    return true
    }
    
    </script>
    Code (markup):

    Here's the navigational element -
    <script type="text/javascript">
    for (i=1; i<Math.ceil(galleryarray.length/totalslots)+1; i++)
    document.write('<a id="navlink'+i+'" href="javascript:jumptopage('+i+')\">Page'+i+'</a> ')
    document.getElementById("navlink1").className="current"
    </script>
    Code (markup):
    Here's some more code from a php file that's also needed for the gallery script..
    <?
    Header("content-type: application/x-javascript");
    
    function returnimages($dirname=".") {
       $pattern="\.(jpg|jpeg|png|gif|bmp)$";
       $files = array();
       $curimage=0;
       if($handle = opendir($dirname)) {
           while(false !== ($file = readdir($handle))){
                   if(eregi($pattern, $file)){
    		 $filedate=date ("M d, Y H:i:s", filemtime($file));
                     echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
                     $curimage++;
                   }
           }
    
           closedir($handle);
       }
       return($files);
    }
    
    echo "var galleryarray=new Array();" . "\n";
    returnimages();
    ?> 
    Code (markup):

    Can anyone tell me where I've gone wrong to get this error and what needs to be rectified..?


    Thanks in advance.
     
    ChannerUK, Sep 25, 2007 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    Where is imgsrc defined ?
     
    it career, Sep 25, 2007 IP
  3. ChannerUK

    ChannerUK Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm totally new to js so haven't got a clue mate.
    It looks as though imgsrc isn't defined anywhere. Can you tell me where it should be? Also, is this why I get the error..?
     
    ChannerUK, Sep 25, 2007 IP
  4. MetaCipher

    MetaCipher Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes imgsrc is defined, in the function. The argument you are passing is, this, in the buildimage function. It should be working, but try this: Instead of passing this to the function, try passing this.href. And in your popuplinkfunc, change imgsrc.href to imgsrc.
     
    MetaCipher, Sep 26, 2007 IP