Adding JS variable to document.anchors["myLink"].href - is it possible?

Discussion in 'Javascript' started by Phil Powell, Apr 27, 2004.

  1. Phil Powell

    Phil Powell Guest

    <script type="text/javascript">
    function addFieldsToLink(link) {
    var qs = ''; // QUERY STRING ADDITION
    var val = '' // VALUE OF FORM ELEMENT;
    for (i = 0; i < document.imageSearchForm.elements.length; i++) {
    if (document.imageSearchFrom.elements.type == 'select-one') {
    val = document.imageSearchForm.elements.options[document.imageSearchForm.elements.selectedIndex].value;
    } else if (document.imageSearchForm.elements.type ==
    'select-multiple') {
    // DO NOT SET val HERE BUT ADD TO QS DIRECTLY
    for (j = 0; j < document.imageSearchForm.elements.options.length;
    j++) {
    qs += '&' + escape(document.imageSearchForm.elements.name) +
    '=' +
    escape(document.imageSearchForm.elements.options[document.imageSearchForm.elements.selectedIndex].value);
    }
    } else if (document.imageSearchForm.elements.type == 'text') {
    val = document.imageSearchForm.elements.value;
    }
    if (val.length() > 0 || document.imageSearchForm.elements.type
    != 'select-multiple') {
    qs += escape(document.SearchForm.elements.name) + '=' + val;
    }
    val = '';
    }
    alert('qs = ' + qs);
    link.href += qs;
    alert(link.href);
    }
    </script>

    <a id="myLink" href="blah.php?stuff=blah&hasStuff=1"
    onClick="addFieldsToLink(this)">Click</a>

    I am trying to create a cross-browser JS function that, upon clicking
    a link, will build upon the existing link an extension query string of
    all form elements that the user enter to retain them in another
    document.

    Unfortunately I cannot verify if this works in Mozilla Firefox nor in
    Konqueror because I am unable to install the JS plug-in successfully
    for Mozilla Firefox and this is a Linux shop (i.e. it's MS-free) so I
    have no ability to use IE to verify errors (and cannot install
    Netscape due to not having root permission to install it.. life is
    grand!).

    So unfortunately, I cannot verify on my own if this works other than
    the fact that I know that at the very least it is not adding the query
    string generated in the JS local variable "val" into link.href in the
    function because the resulting document does not have any additional
    elements that would have been gleaned from the query-string in its
    entirely prior to redirection to that new document upon clicking.

    So now I need some JS help from folks that might have done something
    similar to this, any ideas where I might have gone wrong and what I
    should do to be able to accomplish this task?

    Thanx
    Phil
     
    Phil Powell, Apr 27, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    201,848
    Adrienne Boswell
    Jun 20, 2006
  2. Replies:
    1
    Views:
    2,275
    Pavel Lepin
    Mar 14, 2008
  3. Soren Vejrum
    Replies:
    4
    Views:
    669
    Lasse Reichstein Nielsen
    Jul 5, 2003
  4. Replies:
    2
    Views:
    511
  5. windandwaves
    Replies:
    3
    Views:
    124
    Neo Geshel
    Jan 25, 2007
Loading...

Share This Page