variables in url

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    variables in url

    i am trying to pass the contents of a javascript variable in a url have tried some ideas but cant get it to work, here is the code i am trying to get the url to appear on the page

    Code:
    <script language="javascript">
    						  document.write('<a href="boys.asp?id=all' + select_array + '">')
    						  </script>link</a>
    can anyone help??
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    What's the value of select_array?

    Comment

    • colinod
      Contributor
      • Nov 2007
      • 347

      #3
      the select_array has a numeric value upto three numbers long

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        When do you include this script? Is it after the page has loaded or during page load?

        Comment

        • colinod
          Contributor
          • Nov 2007
          • 347

          #5
          this is after the page load so i can include the varible array in the url to the next page

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            After page load, document.write re-opens the page for writing overwriting older content, or at least the result is unexpected.

            Use DOM methods, e.g. createElement, appendChild, or set the innerHTML property of an element instead.

            Comment

            • colinod
              Contributor
              • Nov 2007
              • 347

              #7
              Hi thanks for the advice i have looked up the createElement and appendChild elements and i think they will do what i want, i just cant figure out how to code them to get the link as i want it

              would it be like this

              Code:
              <script language="javascript">
              function function1(){
                 var myElement = document.createElement('<A href="boys?id=all');
                 myhref.appendChild(myElement);
                 
                 myElement = document.createElement('&select_array"');
                 myhref.appendChild(myElement);
              
                 myElement = document.createElement('linktext</a>');
                 myhref.appendChild(myElement);
              }
              </script>
              <body>
              <button onClick="function1();">link text</button>
              </body>
              </html>
              if not could anyone help on the syntax

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                myhref would have to be a valid reference. You will also need to set the attributes, e.g.
                Code:
                var myElement = document.createElement('A');
                myElement.href="boys?id=all&" + select_array;
                text = document.createTextNode("linktext");
                myElement.appendChild(text);
                myhref.appendChild(myElement);

                Comment

                • colinod
                  Contributor
                  • Nov 2007
                  • 347

                  #9
                  i have tried the above but it comes back saying select_array is undefined, also is there a way of setting an image instead of the link text....thanks

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    I assumed that select_array was set somewhere.

                    You can set an image by creating an IMG element using createElement() . Remember to set its src property to the image source.

                    Comment

                    • colinod
                      Contributor
                      • Nov 2007
                      • 347

                      #11
                      yes the select_array element is set in another function

                      Code:
                      <script language="javascript">
                      function add()
                      {
                      var select_array = new Array();
                      select_array.push(<%=fav%>);
                      select_array.push(<%=actorid%>);
                      alert('You have added <%=yaketyRecordset("firstname")%>' + ' ' + '<%=yaketyRecordset("surname")%> to you casting list');
                      }
                      function boys()
                      {
                      var myElement = document.createElement('A');
                      myElement.href="boys?id=all&" + select_array;
                      text = document.createTextNode("linktext");
                      myElement.appendChild(text);
                      myhref.appendChild(myElement);
                      }
                      </script>

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Either make select_array into a global or return select_array in a function, so that it can be used elsewhere.

                        Also note that myhref will need to be a valid reference. You could use document.body in place of myhref if you just want it appended to the body.

                        Comment

                        • colinod
                          Contributor
                          • Nov 2007
                          • 347

                          #13
                          when you say return select_array in a function can i just put return select_array at the beginning of the function tp get it in??? not sure what you mean by global!

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            See Variables for an explanation. Strictly speaking, they're properties of the window object. Normally, you'd avoid global variables, but in a simple page (especially where you're fairly new to JavaScript), it shouldn't be too much of a problem.

                            Comment

                            Working...