Modifying a Script

Discussion in 'JavaScript' started by Daniel591992, Feb 21, 2009.

  1. #1
    Hi, hopefully someone can help.

    I found a script that can display a series of images by modifying the url. For example, if site.com/img[1021-1042].jpg was entered in the form, those images would all be displayed on one page. I have it modified so that if site.com/story.php?page=[1-4] is entered, pages 1-4 will display. The problem is that, in many cases, ?page=1 is the second page, and story.php is the first page.

    The script is not advanced enough to list all the pages. So what I need to do is modify it so that I can display all the pages. In my opinion, the best way of doing that would be like this:
    http://site.com/story.php | http://site.com/story.php?page=[1-4]
    Code (markup):
    That way, the first page is posted as long with the group of pages.

    How can I change the following code to do that?
    +Rep to anyone that helps. :)

    
        <SCRIPT LANGUAGE=javascript>
        <!--
    
        // ---------------------------------------------------------------------------
        // -- Scriptname   : Javascript Fusker                                      --
        // -- Author       : Bas de Reuver                                          --
        // -- Date         : 8-may-2004                                             --
        // -- Description  : Sometimes someone put up lots of pictures (Pic001.jpg, --
        // --                Pic002.jpg etc.) without a proper overview or index    --
        // --                page. This script takes an URL and dumps all pictures  --
        // --                in the HTML document so you can view them more easily. --
        // --                Idea sort of taken from http://fusker.lewww.com        --
        // --                fusker: [Danish], n. cheater, hacker                   --
        // -- 14-jul-2004 Johann Zacharee sent in some updates and ideas            --
        // -- added nested number ranges and removed hardcoded "fusker.html" name.  --
        // ---------------------------------------------------------------------------
    
        function GenerateContent()
        {
          var strParameter = window.location.search;
    //    if (strParameter.charAt (0) != '?')
    //      return(1);
    
          strParameter = strParameter.substring(1, strParameter.length);
    
          //next link has info on 'command-line' arguments
          //http://sharkysoft.com/tutorials/jsa/content/043.html
    
          if (strParameter == '')
          {
          }
          else
          {
            // must contain 'http://' or 'ftp://' or something
            if (strParameter.indexOf('://') < 0)
              {strParameter = 'http://' + strParameter}
    
            strParameter = strParameter.replace(/%5B/g, '[');  // g=global, replace all
            strParameter = strParameter.replace(/%5D/g, ']');
    
            document.form1.URL.value = strParameter;
            ProcessURL('', strParameter); // start recursion
          }
        }
    
        function RemoveLeadingZeros(strInput)
        {
          // remove zero's because else parseInt('0123') interprets as octal number and returns 83
          i = 0
          while (strInput.substr(i, 1) == '0') {i++}
    
          // incase all zero's then the string is empty now
          if (strInput.length == i)
            // put one '0' character in string
            {strInput = '0'}
          else
            // removeonly keep valid digits '00123' -> '123'
            {strInput = strInput.substr(i)}
          return strInput;
        }
    
        function WriteURLtoDocument(strURL)
        {   
          document.writeln('<div class="idiv"><a href="' + strURL + '">' + strURL + '</a>');
          document.writeln('<iframe src="' + strURL + '"></iframe></div>');
        }
    
        // ExamineURL(strFirst, strLast) takes an URL and determines where the number-part is located.
        // example: strFirst='http://www.bla.com/user/test[01-10].jpg'
        // will show pictures test01.jpg through test10.jpg
        function ProcessURL(strFirstPart, strLastPart)
        {
          var strBegin = '';
          var strEnd = '';
          var iStartNr = -1;
          var iEndNr = -1;
          var iDigits = 0  // number of digits, example 3 digits, then 1 becomes '001'
          var strTemp;
    
          var strNumberPart = '';
          var i;
    
          //force typecast to string
          strFirstPart = strFirstPart + '';
          strLastPart = strLastPart + '';
    
          // check for '[01-10]' part
          var iBegin = strLastPart.indexOf('[');
          var iEnd   = strLastPart.indexOf(']');
          if (iBegin < 0 || iEnd < 0)
          {
            // no more number parts, print to HTML document
            WriteURLtoDocument(strFirstPart + strLastPart);
            return -1;
          };
    
          // there are more number parts, process it
          strBegin = strFirstPart + strLastPart.substr(0, iBegin);
          strEnd   = strLastPart.substr(iEnd+1, strLastPart.length-iEnd);
    
          var strTemp = strLastPart.substr(iBegin+1, (iEnd-iBegin-1));
          var iDash  = strTemp.indexOf('-');
    
          if (iDash < 0)
          {
            WriteURLtoDocument(strFirstPart + strLastPart);
            return -1;
          };
    
          var strStartNr = strTemp.substr(0, iDash);
          iDigits  = strStartNr.length;
          strStartNr = RemoveLeadingZeros(strStartNr);
    
          var strEndNr = strTemp.substr(iDash+1, strTemp.length-iDash-1);
          strEndNr = RemoveLeadingZeros(strEndNr);
    
          if (isNaN(strStartNr) == true || isNaN(strEndNr) == true)
          {
            WriteURLtoDocument(strFirstPart + strLastPart);
            return -1;
          }
    
          iStartNr = parseInt(strStartNr);
          iEndNr = parseInt(strEndNr);
    
          // call ProcessURL recursively
          for (i = iStartNr; i <= iEndNr; i++)
          {
            strNr = i + '';  // typecast to string
            while (strNr.length < iDigits)
              {strNr = '0' + strNr}
            // recursive call
            strTemp = strBegin + strNr;
            ProcessURL(strTemp, strEnd);
          }
        }
    
    
        function HandleSubmit()
        {
          // when user types in textbox and presses enter, this function is called
          var strParameter = document.form1.URL.value;
          // replace again, easier to copy into UBB codes ([url=..]) because that also uses '[' and ']'
          strParameter = strParameter.replace(/\[/g, '%5B'); // g=global, replace all
          strParameter = strParameter.replace(/\]/g, '%5D');
          top.location = window.location.pathname + '?' + strParameter;
          // next lines causes form not to reload (like pressing F5)
          return false;
        }
    
        //-->
        </SCRIPT>
    
    Code (markup):
     
    Daniel591992, Feb 21, 2009 IP