Results 1 to 8 of 8

Thread: window.location not working

  1. #1
    Join Date
    Mar 2005
    Location
    Mumbai,INDIA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default window.location not working

    here is my code,
    Code:
    <html><body>
    <form name="jumpurl1" onSubmit="return jumpit()">
    <input type="text" size=30 name="jumpurl2" value="http://">
    <select name="jumpurl3">
    <option>select</option>
    <option>.com</option>
    <option>.cjb.net</option></select>
    <input type="button" value="Go!" onClick="jumpit()">
    </form>
    <script>
    function jumpit(){
    window.location=document.jumpurl1.jumpurl2.jumpurl3.value
    return false
    }
    </script>
    </body></html>
    this script works when jumpurl3 is absent from window.location statement.but in it's present it doesn't work. Can any one tell why?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because document.jumpurl1.jumpurl2.jumpurl3 doesn't exist.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>A title</title>
      </head>
      <body>
        <form action="?" onsubmit="
          window.location.href =
            this.elements['jumpurl2'].value +
            this.elements['jumpurl3'].value;
        ">
          <p>
            <input type="text" size="30" name="jumpurl2" value="http://..."  style="
              color: silver;
              background-color: white;
            "
            onfocus="
              with(this) {
                if(!first) {
                  value = '';
                  style.color='black';
                  first = true;
                }
              }
            ">
            <select name="jumpurl3">
              <option value="">select</option>
              <option>.com</option>
              <option>.cjb.net</option>
            </select>
            <input type="submit" value="Go!">
          </p>
        </form>
      </body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    Mumbai,INDIA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    well,
    The outcome of the script which you gave was:
    file:///C:/WINDOWS/Desktop/123.html?jumpurl2=http%3A%2F%2Fwww.yahoo&jumpurl3=.com
    when i typed yahoo and selected .com as option.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    OK, try this:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>A title</title>
      </head>
      <body>
        <form action="?" onsubmit="
          window.location.href =
            "http://" +
            this.elements['jumpurl2'].value +
            this.elements['jumpurl3'].value;
        ">
          <p>
            <input type="text" size="30" name="jumpurl2" value="http://..."  style="
              color: silver;
              background-color: white;
            "
            onfocus="
              with(this) {
                if(!first) {
                  value = '';
                  style.color='black';
                  first = true;
                }
              }
            ">
            <select name="jumpurl3">
              <option value="">select</option>
              <option>.com</option>
              <option>.cjb.net</option>
            </select>
            <input type="submit" value="Go!">
          </p>
        </form>
      </body>
    </html>
    Oh, and it ought to be noted that cjb.net isn't a top-level domain
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Oh, and it ought to be noted that cjb.net isn't a top-level domain
    Yeh, it is a redirector, but in the form of a sub domain.

  6. #6
    Join Date
    Mar 2005
    Location
    Mumbai,INDIA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Still the same output.Anyways,thanks for your replies

  7. #7
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Try This

    Code:
    <html><body>
    <form name="jumpurl1" onSubmit="return jumpit()">
    <input type="text" size=30 name="jumpurl2" value="http://">
    <select name="jumpurl3">
    <option value="">select</option>
    <option value=".com">.com</option>
    <option value=".cjb.net">.cjb.net</option></select>
    <input type="button" value="Go!" onClick="jumpit()">
    </form>
    <script>
    function jumpit(){
    window.location=document.jumpurl1.jumpurl2.value + document.jumpurl1.jumpurl3.value
    return false
    }
    </script>
    </body></html>



    Your code had two problems
    1) jumpurl3 is not a child of jumpurl2
    2) you didnt put values in options


    I know its too late but may help someone else

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This thread is pretty ancient. Here's how I'd write it now:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>A title</title>
      </head>
      <body>
        <form action="jumpurl.cgi" onsubmit="
          var e = this.elements,
              u = e.urlstring.value,
              d = e.urldom.value;
          return !!(u && d
            && (u.defaultValue !== u.value)
            && (this.action = u + d));
        ">
          <p>
            <input type="text" name="urlstring" value="http://..."  style="
              color: silver;
              background-color: white;
            "
            onfocus="
              this.value === this.defaultValue && (this.value = '');
              this.style.color = 'black';
            "
            onblur="
              if(!this.value) {
                this.value = this.defaultValue;
                this.style.color = 'silver';
              }
            ">
            <select name="urldom">
              <option value="">(select one)</option>
              <option value=".com">.com</option>
              <option value=".cjb.net">.cjb.net</option>
            </select>
            <input type="submit" value="Go!">
          </p>
        </form>
      </body>
    </html>
    ... where "jumpurl.cgi" should be a server-side script capable of taking the form values and redirecting the user to the appropriate page, without Javascript.
    <html><body>
    Your document is missing a DOCTYPE and its <head> tag.
    <form name="jumpurl1" onSubmit="return jumpit()">
    <input type="text" size=30 name="jumpurl2" value="http://">
    A block-level element is required here. Also, the name attribute is deprecated for this purpose.
    <script>
    The <script> element requires a type attribute.
    window.location=document.jumpurl1.jumpurl2.value + document.jumpurl1.jumpurl3.value
    Accessing form elements this way is deprecated and may cause problems depending on the element names (you should use the forms and elements collections). You also (as a matter of good coding style) should try to use semicolons to unambiguously separate one statement from the next. The Javascript parser, if there are no semicolons, will attempt to form the longest statement possible, and this isn't always what you'd expect. Consider:
    Code:
    var a = 2
    (new Foo()).bar()
    This will throw an error "2 is not a function," because the interpreter will understand it as:
    Code:
    var a = 2(new Foo()).bar();
    rather than the (probably expected)
    Code:
    var a = 2;
    (new Foo()).bar();
    Your code had two problems
    1) jumpurl3 is not a child of jumpurl2
    How is that a problem? There's no way one <input> can be the child of another :-\
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •