Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help on a simple code

    I have a copy of this somewhere and it's programming 101, but can't find it and could use some help.

    Suppose I have a domain, as an example: www.mydomain.com/members.

    I want my members to go to my home page and type in their username in a field and click enter. At this point, they would be taken to www.mydomain.com/members/username.

    Could someone who is not laughing at the simplicity give me a clue. I must be getting old.

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by epsaothy View Post
    Suppose I have a domain, as an example: www.mydomain.com/members.

    I want my members to go to my home page and type in their username in a field and click enter. At this point, they would be taken to www.mydomain.com/members/username.

    Could someone who is not laughing at the simplicity give me a clue.
    Use location.assign("username/"). Or location.assign("username") if username is really a file and not a directory.

    Code:
    var form = [...];
    function redirect(formSubmissionEvent) {
    	formSubmissionEvent.preventDefault(); // Prevent form submission.
    	location.assign("username/");
    }
    form.addEventListener("submit", redirect);
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx much. This is different than what I used to have. It's much more sophisticated. Sorry, could you help with the entire html code?

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by epsaothy View Post
    Thanx much. This is different than what I used to have. It's much more sophisticated. Sorry, could you help with the entire html code?
    You need to post your code and describe the problem you need solved. If it's not directly related to the original question posed, I would start a new thread.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    <input type="text" id="username" />
    <input type="button" id="btnGo" value="Go!" />
    <script type="text/javascript">
        document.getElementById('btnGo').onclick = function() [
            var username = document.getElementById('username');
            if (username.value) {
                location.href = username.value;
            }
        }
    </script>

  • #6
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanx all for your help.

    glenngv, I put that on a page and nothing happens when I try to use it. Is there something I'm missing?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by epsaothy View Post
    thanx all for your help.

    glenngv, I put that on a page and nothing happens when I try to use it. Is there something I'm missing?
    Check your error console. There is a typo in glenn's code:-

    document.getElementById('btnGo').onclick = function() {


    var username = document.getElementById('username');

    FWIIW I would prefer not to use the same name for an HTML element and a Javascript variable.
    Last edited by Philip M; 02-14-2014 at 04:40 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,623
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Philip M View Post
    FWIIW I would prefer not to use the same name for an HTML element and a Javascript variable.
    It is safe to do so if the code is inside a function and therefore is not in global scope.

    The code can break in some browsers when the code is in global scope where the browser can populate global fields itself that you don't expect. Therefore when the code is in global scope you MUST use a different name or it will likely be broken in some browsers.

    Of course JavaScript should be written so that at most one variable has global scope (to pass the framework into your function) and then the name doesn't matter as it is no longer global.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by felgall View Post
    It is safe to do so if the code is inside a function and therefore is not in global scope.

    The code can break in some browsers when the code is in global scope where the browser can populate global fields itself that you don't expect. Therefore when the code is in global scope you MUST use a different name or it will likely be broken in some browsers.

    Of course JavaScript should be written so that at most one variable has global scope (to pass the framework into your function) and then the name doesn't matter as it is no longer global.
    Yes, but I did say prefer. Common sense says that you do not give two similar things (dogs, children, Javascript variables) the same name. It is obviously possible to have two or more local variables in different functions with the same name. But poor practice IMO. The only exceptions would be counters, (i,j,k).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Philip M, your correction does work, but what I'm needing is to be able to have the domain url inside the code.

    For example, I want someone to be directed to another directory: If the address I want them to goto is mydomain.com/member/username, I want them to type in their username and be taken the member directory.


    Thanx

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by epsaothy View Post
    Philip M, your correction does work, but what I'm needing is to be able to have the domain url inside the code.

    For example, I want someone to be directed to another directory: If the address I want them to goto is mydomain.com/member/username, I want them to type in their username and be taken the member directory.


    Thanx
    Code:
    <script type="text/javascript">
    document.getElementById('btnGo').onclick = function() {
    var uname = document.getElementById('username');
    if (uname.value) {
    var loc = "http://www.mydomain.com/members/" + uname.value;
    location.href = loc;
    }
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx. That was it. It works perfectly now.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Does the username allow special characters such as ?, &, =, /, space, accents, and the like?

    You need to encode the username when appending to the URL.

    Code:
    var loc = "http://www.mydomain.com/members/" + encodeURI(uname.value);


  •  

    Posting Permissions

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