Javascript Replace()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rsmastermind
    New Member
    • Sep 2008
    • 93

    #31
    Yes this is an error u need to close all the braces If the brace will be opened the javascript will not get executed.

    Comment

    • risk32
      New Member
      • Mar 2008
      • 98

      #32
      OK, looked at the IE error report is put out, though the line numbers arent the exact number is says. I'm pretty sure it's throwing an error due to the "downloadAr ea" portion of it. I've gotten it to work before, but after that, I had to throw in the IF/ELSE statement... might just be syntax and/or placement issues.
      Code:
      <html>
      <head>
      <script>
      var year ="2008";
      var fileExt = ".ppt";
      function getRadioButtonvalue(radioButtons)
      {
      var value = null;
      for (var i=0; i<= radioButtons.length -1; i++)
      {
      if (radioButtons[i].checked)
      {
      value =radioButtons[i].value;
      }
      }
      return value;
      }
      function displayFileName()
       { 
      var type = getRadioButtonValue(document.getElementsByName("schedule"));  
      var downloadArea = document.getElementById("downloadArea"); 
      downloadArea.innerHTML = '<a href="'+fileName+'"> Click to download File </a>';    
       if (type == "schedule") { 
          var day = getRadioButtonvalue(document.getElementsByName("day"));  
          var month = getRadioButtonvalue(document.getElementsByName("month")); 
          var filePath = "M:\\data\\Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008\\" 
          var fileName = filePath +  month + "\\" + day + " " + month + " " + year + fileExt; 
      } else {
      var filePath = "M:\\data\\Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008\\INFO & STATUS\\" 
      var fileName = filePath + "Info And Status" + fileExt;
      </script>
      </HEAD>
      <BODY>
      <TABLE cellSpacing="0" cellPadding="0" width="1111" bgColor="#999999" border="2">
      <h5>Please Select Airfield Status Or Daily Schedule</h5>
      <input type="radio" name="type" value="INFO & STATUS\Info and Status.ppt">Airfield Status<br>
      <input type="radio" name="type" value="">Daily Schedule
      <h5>Please Select the Day<br></h5>
      <input type="radio" name="day" value="01">01
      <input type="radio" name="day" value="02">02
      <input type="radio" name="day" value="03">03
      <input type="radio" name="day" value="04">04
      <input type="radio" name="day" value="05">05
      <input type="radio" name="day" value="06">06
      <input type="radio" name="day" value="07">07
      <input type="radio" name="day" value="08">08
      <input type="radio" name="day" value="09">09
      <input type="radio" name="day" value="10">10
      <input type="radio" name="day" value="11">11
      <input type="radio" name="day" value="12">12
      <input type="radio" name="day" value="13">13
      <input type="radio" name="day" value="14">14
      <input type="radio" name="day" value="15">15<br>
      <input type="radio" name="day" value="16">16
      <input type="radio" name="day" value="17">17
      <input type="radio" name="day" value="18">18
      <input type="radio" name="day" value="19">19
      <input type="radio" name="day" value="20">20
      <input type="radio" name="day" value="21">21
      <input type="radio" name="day" value="22">22
      <input type="radio" name="day" value="23">23
      <input type="radio" name="day" value="24">24
      <input type="radio" name="day" value="25">25
      <input type="radio" name="day" value="26">26
      <input type="radio" name="day" value="27">27
      <input type="radio" name="day" value="28">28
      <input type="radio" name="day" value="29">29
      <input type="radio" name="day" value="30">30
      <input type="radio" name="day" value="31">31
      <h5>Please Select the month</h5>
      <input type="radio" name="month" id="Janauary" value="jan">January<br>
      <input type="radio" name="month" id="February" value="feb">February<br>
      <input type="radio" name="month" id="March" value="mar">March<br>
      <input type="radio" name="month" id="April" value="apr">April<br>
      <input type="radio" name="month" id="May" value="may">May<br>
      <input type="radio" name="month" id="June" value="jun">June<br>
      <input type="radio" name="month" id="July" value="jul">July<br>
      <input type="radio" name="month" id="August" value="aug">August<br>
      <input type="radio" name="month" id="September" value="sep">September<br>
      <input type="radio" name="month" id="October" value="oct">October<br>
      <input type="radio" name="month" id="November" value="nov">November<br>
      <input type="radio" name="month" id="December" value="dec">December<br>
      <br>
      <H3>Please Select the Appropiate Link</H3>
      <input type="button" value="Generate Link" onClick="displayFileName()">
      <input type="reset" value="reset" onclick="window.location.reload(true); return false;">
      <div id="downloadArea"></div>
      </body>
      </html>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #33
        Change it back to what you had before. The download Area lines have to appear after the if/else statement because that's where the fileName variable is set.

        What you needed to do was add two brackets. One to close the else and one to close the function. Whenever you have an opening { you need a matching close }.

        One final thing: don't trust IE error message line numbers. They're very often horribly wrong. First test in a decent other browser such as Firefox or Opera, then finally test in IE.

        Comment

        • risk32
          New Member
          • Mar 2008
          • 98

          #34
          Originally posted by acoder
          Change it back to what you had before. The download Area lines have to appear after the if/else statement because that's where the fileName variable is set.

          What you needed to do was add two brackets. One to close the else and one to close the function. Whenever you have an opening { you need a matching close }.

          One final thing: don't trust IE error message line numbers. They're very often horribly wrong. First test in a decent other browser such as Firefox or Opera, then finally test in IE.
          Unfortunately I'm stuck with the POS IExplorer. I changed it around slightly, put the downloadArea at the end, and made sure I put closing brackets after each section containing an opening bracket. Still throwing errors with IE. I'm pretty sure it's either that it's not getting the element ID from the bottom of the page, where <div id="downloadAre a"> is specified. I'm doing something wrong...
          Code:
          function getRadioButtonvalue(radioButtons) 
          { 
          var value = null; 
          for (var i=0; i<= radioButtons.length -1; i++) 
          { 
          if (radioButtons[i].checked) 
          { 
          value =radioButtons[i].value; 
          } 
          } 
          return value; 
          } 
          
          
          function displayFileName() 
          { 
              var type = getRadioButtonValue(document.getElementsByName("schedule"));
              var day = getRadioButtonvalue(document.getElementsByName("day")); 
              var month = getRadioButtonvalue(document.getElementsByName("month")); 
                var downloadArea = document.getElementById("downloadArea");
          }
            if (type == "schedule") 
          {
              var filePath = getRadioButtonvalue(document.getElementsByName("month")); 
              var fileName = filePath + "/" + day + " " + month + " " + year + fileExt;
          } 
          {
          else  
          var filePath = "M:\\data\\Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008\\INFO & STATUS\\"  
          var fileName = filePath + "Info And Status" + fileExt; 
          }
          {
              //write to page.. 
              
              downloadArea.innerHTML = '<a href="'+fileName+'"> Click to download File </a>';
          }
          </script>
          <html> ......
          <div id"downloadArea">
          </html>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #35
            There are a number of syntax errors.

            On line 21, you close the function displayFileName which is not what you want, so remove that line. Lines 27/28 need to be swapped round because the if-else syntax is "if { ... } else { ... }", not "if { ... } { else }". Finally, on line 32, you're opening a block of code with { which is not required, so remove that line too.

            Comment

            • risk32
              New Member
              • Mar 2008
              • 98

              #36
              Originally posted by acoder
              There are a number of syntax errors.

              On line 21, you close the function displayFileName which is not what you want, so remove that line. Lines 27/28 need to be swapped round because the if-else syntax is "if { ... } else { ... }", not "if { ... } { else }". Finally, on line 32, you're opening a block of code with { which is not required, so remove that line too.
              Alright, IE is pretty much ticking me off. If I wasn't at work i could be getting better error reports from FireFox. I've done all the tips that you've given me, looked over my code a hundred times to see if there's something I'm missing and it's still not working. I'm about to say screw it and create an EXE using VB. GUI's most generally work better than IE anywyas.
              Code:
              <html>
              <head>
              <script>
              var year ="2008";
              var fileExt = ".ppt";
              function getRadioButtonvalue(radioButtons)  
              {  
              var value = null;  
              for (var i=0; i<= radioButtons.length -1; i++)  
              {  
              if (radioButtons[i].checked)  
              {  
              value =radioButtons[i].value;  
              }  
              }  
              return value;  
              }  
              function displayFileName()  
              {  
                  var type = getRadioButtonValue(document.getElementsByName("schedule")); 
                  var day = getRadioButtonvalue(document.getElementsByName("day"));  
                  var month = getRadioButtonvalue(document.getElementsByName("month"));  
                    var downloadArea = document.getElementById("downloadArea"); 
                if (type == "schedule")  
               {
                  var filePath = getRadioButtonvalue(document.getElementsByName("month"));  
                  var fileName = filePath + "/" + day + " " + month + " " + year + fileExt; 
              }  
              else
              {   
              var filePath = "M:\\data\\Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008\\INFO & STATUS\\"   
              var fileName = filePath + "Info And Status" + fileExt;  
              } 
              //write to page..  
              downloadArea.innerHTML = '<a href="'+fileName+'"> Click to download File </a>'; 
              </script>
              </HEAD>
              <BODY>
              <TABLE cellSpacing="0" cellPadding="0" width="1111" bgColor="#999999" border="2">
              <h5>Please Select Airfield Status Or Daily Schedule</h5>
              <input type="radio" name="type" value="INFO & STATUS\Info and Status.ppt">Airfield Status<br>
              <input type="radio" name="type" value="">Daily Schedule
              <h5>Please Select the Day<br></h5>
              <input type="radio" name="day" value="01">01
              <input type="radio" name="day" value="02">02
              <input type="radio" name="day" value="03">03
              <input type="radio" name="day" value="04">04
              <input type="radio" name="day" value="05">05
              <input type="radio" name="day" value="06">06
              <input type="radio" name="day" value="07">07
              <input type="radio" name="day" value="08">08
              <input type="radio" name="day" value="09">09
              <input type="radio" name="day" value="10">10
              <input type="radio" name="day" value="11">11
              <input type="radio" name="day" value="12">12
              <input type="radio" name="day" value="13">13
              <input type="radio" name="day" value="14">14
              <input type="radio" name="day" value="15">15<br>
              <input type="radio" name="day" value="16">16
              <input type="radio" name="day" value="17">17
              <input type="radio" name="day" value="18">18
              <input type="radio" name="day" value="19">19
              <input type="radio" name="day" value="20">20
              <input type="radio" name="day" value="21">21
              <input type="radio" name="day" value="22">22
              <input type="radio" name="day" value="23">23
              <input type="radio" name="day" value="24">24
              <input type="radio" name="day" value="25">25
              <input type="radio" name="day" value="26">26
              <input type="radio" name="day" value="27">27
              <input type="radio" name="day" value="28">28
              <input type="radio" name="day" value="29">29
              <input type="radio" name="day" value="30">30
              <input type="radio" name="day" value="31">31
              <h5>Please Select the month</h5>
              <input type="radio" name="month" id="Janauary" value="jan">January<br>
              <input type="radio" name="month" id="February" value="feb">February<br>
              <input type="radio" name="month" id="March" value="mar">March<br>
              <input type="radio" name="month" id="April" value="apr">April<br>
              <input type="radio" name="month" id="May" value="may">May<br>
              <input type="radio" name="month" id="June" value="jun">June<br>
              <input type="radio" name="month" id="July" value="jul">July<br>
              <input type="radio" name="month" id="August" value="aug">August<br>
              <input type="radio" name="month" id="September" value="sep">September<br>
              <input type="radio" name="month" id="October" value="oct">October<br>
              <input type="radio" name="month" id="November" value="nov">November<br>
              <input type="radio" name="month" id="December" value="dec">December<br>
              <br>
              <H3>Please Select the Appropiate Link</H3>
              <input type="button" value="Generate Link" onClick="displayFileName()">
              <input type="reset" value="reset" onclick="window.location.reload(true); return false;">
              <div id="downloadArea"></div>
              </body>
              </html>
              I'm pretty sure if you save my code to an html and run it, you'll definately have the idea of what i'm trying to do, but the filePath's definately wont work since I'm on a private network. Don't worry though. Security isn't such a big thing for this project.

              Thanks for the help acoder

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #37
                I notice a few more errors. You haven't closed displayFileName properly. There should be a closing } after the last line of the function before </script>.

                The type value is incorrect because you're getting the radio button value of elements named "schedule" instead of "type". Secondly, you will get an error because JavaScript is case-sensitive and you calling the getRadioButtonv alue() function with a capital V instead of a lower-case v.

                See if that helps fix things.

                Comment

                • risk32
                  New Member
                  • Mar 2008
                  • 98

                  #38
                  Originally posted by acoder
                  I notice a few more errors. You haven't closed displayFileName properly. There should be a closing } after the last line of the function before </script>.

                  The type value is incorrect because you're getting the radio button value of elements named "schedule" instead of "type". Secondly, you will get an error because JavaScript is case-sensitive and you calling the getRadioButtonv alue() function with a capital V instead of a lower-case v.

                  See if that helps fix things.
                  Alright, looks like it worked it's wonders.
                  Now all I've got to do is make sure every single .ppt file is in the correct format to open it according to the link. I've got all kinds of different ways people have saved these slides... Ugh... :( But that comes with the terriroty of being a Computer Support Assistant AND being a Duty Officer for a C-130 squadron.

                  Is there a way to add a login form and change visible to false until the correct login is input? Also, without a server-side ability, is there a way to add cookies using a mapped network drive and fake a folder to act as a server? Reason I'm asking... we have a network server, with no direct access to the configuration or software on it iself. Most file extensions are blocked except those savable under Microsoft office, html, and .js files. ALL .exes are blocked as well as .bats
                  Is there a way to write it in javascript to fake itself to create an object to act as a server for the cookies function? We've got 180 people and some higher ups that need access to this webpage for historical purposes and having MySQL is just not an option for maintaining usernames and password for it. It's not classified information, but still need to prevent anyone not part of our office looking at it.

                  I greatly appreciate your help. Without it I'd be pulling my hair out of just giving up on this html. With the guidance you've given, I understand JS a little more and realize how strict the language is, unlike VB or html. Again, thank you.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #39
                    I'm afraid that won't be possible. You can use cookies without a server, but for any type of login security, you will need server-side code. JavaScript doesn't offer much protection. Any that it does is easily circumvented by anyone with even a bit of knowledge.

                    What type of server/network is it? There may be options that you could consider, but they wouldn't involve JavaScript, so you can try asking in an appropriate forum, or Misc. Questions if nothing fits.

                    Personally, I would consider a proper server setup which allows you to control access.

                    Anyway, glad to hear that you've got the original problem solved. You could improve it further by using select elements instead of radio buttons.

                    Comment

                    • risk32
                      New Member
                      • Mar 2008
                      • 98

                      #40
                      Originally posted by acoder
                      I'm afraid that won't be possible. You can use cookies without a server, but for any type of login security, you will need server-side code. JavaScript doesn't offer much protection. Any that it does is easily circumvented by anyone with even a bit of knowledge.

                      What type of server/network is it? There may be options that you could consider, but they wouldn't involve JavaScript, so you can try asking in an appropriate forum, or Misc. Questions if nothing fits.

                      Personally, I would consider a proper server setup which allows you to control access.

                      Anyway, glad to hear that you've got the original problem solved. You could improve it further by using select elements instead of radio buttons.
                      The server being used is operated by outside personnel. There's no way I could get any administrative rights to it. And any software installed has to be coordinated with proper approval before anything gets installed... welcome to the military. Kind of like submitting a form to request a form to get the form you really need. With the programming aspect, I wish I was still a civilian. I could get a lot more done and have a lot more time to work with it.

                      Anyways, I tried using the select elements for a drop down menu with JS but it was to no avail. I couldn't get the element values to cross over into each menu and there would be 365+ option values.... WAY too much coding when I could use radio buttons like this. Unless of course, all I have to do in this code is substitute the radio button for a select. Also, the site is working just fine, just need to keep maintenance contantly since most people don't know how to save the .ppt files the right way... Also implemented an Excel VBA project that does the same thing, just trying to transfer it over to VB6 for compiling. Hopefully this gets some good feedback for the work I've been doing to improve operations.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #41
                        To use select elements, replace the radio buttons with select/option elements in the HTML, e.g. for day:
                        Code:
                        <select id="day">
                        <option value="01">01
                        then in displayFileName (), replace the lines setting the day and month to get the select values instead, e.g. for day:
                        Code:
                        var day = document.getElementById("day").value;

                        Comment

                        • risk32
                          New Member
                          • Mar 2008
                          • 98

                          #42
                          Originally posted by acoder
                          To use select elements, replace the radio buttons with select/option elements in the HTML, e.g. for day:
                          Code:
                          <select id="day">
                          <option value="01">01
                          then in displayFileName (), replace the lines setting the day and month to get the select values instead, e.g. for day:
                          Code:
                          var day = document.getElementById("day").value;
                          When I transferred everything over for a <select> the coding is giving me an error. IE is saying that "Object doesn't support this proprty or method". Maybe Firefox could give me some better advice? I must apologize for asking for all of this help. I'm stuck using IE and notepad :*(

                          Anyways, I separated the code into a .js and .html, mostly so I dont have to keep looking at the same code over and over. I can fix one or both and not have to worry so much.

                          Here's the .js, called link.js (nothing really changed since the last time I posted it)
                          Code:
                          var year ="2008"; 
                          var fileExt = ".ppt"; 
                          function getRadioButtonvalue(radioButtons)   
                          {   
                          var value = null;   
                          for (var i=0; i<= radioButtons.length -1; i++)   
                          {   
                          if (radioButtons[i].checked)   
                          {   
                          value =radioButtons[i].value;   
                          }   
                          }   
                          return value;   
                          }   
                          function displayFileName()   
                          {   
                              var type = getRadioButtonvalue(document.getElementsByName("schedule"));  
                              var day = document.getElementsById("day");   
                              var month = document.getElementById("month");   
                                var downloadArea = document.getElementById("downloadArea");  
                            if (type == "schedule")   
                           { 
                              var filePath = "\\Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008"
                              var fileName = filePath + "/" + month + "/" + day + " " + month + " " + year + fileExt;  
                          }   
                          else 
                          {    
                          var filePath = "Duty Desk\\Electronic Boards Info\\Schedule Slides\\2008\\INFO & STATUS\\"    
                          var fileName = filePath + "Info And Status" + fileExt;   
                          }  
                          //write to page..   
                          downloadArea.innerHTML = '<a href="'+fileName+'"> Click to download File </a>';  
                          }
                          The .html file...
                          Code:
                          <html>
                          <BODY>
                          <script type="text/javascript" src="link.js">
                          </script>
                          <TABLE cellSpacing="0" cellPadding="0" width="1111" bgColor="#999999" border="2">
                          <h5>Please Select Airfield Status Or Daily Schedule</h5>
                          <input type="radio" name="airfield" value="airfield">Airfield Status<br>
                          <input type="radio" name="schedule" value="schedule">Daily Schedule
                          <h5>Please Select the Day<br></h5>
                          <select id="day">
                          <option value="01">01
                          <option value="02">02
                          <option value="03">03
                          <option value="04">04
                          <option value="05">05
                          <option value="06">06
                          <option value="07">07
                          <option value="08">08
                          <option value="09">09
                          <option value="10">10
                          <option value="11">11
                          <option value="12">12
                          <option value="13">13
                          <option value="14">14
                          <option value="15">15
                          <option value="16">16
                          <option value="17">17
                          <option value="18">18
                          <option value="19">19
                          <option value="20">20
                          <option value="21">21
                          <option value="22">22
                          <option value="23">23
                          <option value="24">24
                          <option value="25">25
                          <option value="26">26
                          <option value="27">27
                          <option value="28">28
                          <option value="29">29
                          <option value="30">30
                          <option value="31">31
                          </select>
                          <h5>Please Select the month</h5>
                          <select id="month">
                          <option value="january">January
                          <option value="february">February
                          <option value="march">March
                          <option value="april">April
                          <option value="may">May
                          <option value="june">June
                          <option value="july">July
                          <option value="august">August
                          <option value="september">September
                          <option value="october">October
                          <option value="november">November
                          <option value="decemeber">December
                          </select>
                          <br>
                          <H3>Please Select the Appropiate Link</H3>
                          <input type="button" value="Generate Link" onClick="displayFileName()">
                          <input type="reset" value="reset" onclick="window.location.reload(true); return false;">
                          <div id="downloadArea"></div>
                          </body>
                          </html>
                          Honestly... I have no clue as to why it would throw an error like that.. but then, I'm also very new to javascripting.

                          Thanks again,
                          Adam

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #43
                            Lines 18 and 19 in the JavaScript should be set to the value, not the element:
                            Code:
                            var day = document.getElementsById("day").value;

                            Comment

                            • risk32
                              New Member
                              • Mar 2008
                              • 98

                              #44
                              Originally posted by acoder
                              Lines 18 and 19 in the JavaScript should be set to the value, not the element:
                              Code:
                              var day = document.getElementsById("day").value;
                              Looks like it fixed the problem. THANKS A BUNCH!

                              Comment

                              • acoder
                                Recognized Expert MVP
                                • Nov 2006
                                • 16032

                                #45
                                You're welcome. Glad it's working :) I'm sure you (and your users) will find select elements easier to deal with than radio buttons.

                                Comment

                                Working...