www.webdeveloper.com
Results 1 to 6 of 6

Thread: Redirecting if there is no query string

  1. #1
    Join Date
    Jan 2008
    Posts
    26

    Redirecting if there is no query string

    Hi, I want some code that redirects a page if the URL doesn't contain a query string. A query string (to me) is "?key=value&anotherKey=anotherValue". It goes at the very end of a URL. I want it so even if there is only an "?" or only one name=value pair, that will still not redirect it. In other word, only redirect if there is no trace of a query string, not even an "?".

  2. #2
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    The window.location.search provides the query as a string, including the ? mark.

    Code:
    if (window.location.search.length <= 1) {
      window.location.href = "pagetoredirectto.html";
    }

  3. #3
    Join Date
    Jan 2008
    Posts
    26

    New problem...

    Ok, there is a new problem. Here is the code:
    HTML Code:
    <body onLoad="redirect();init();verify();">
    
    <script type="text/javascript">
    function redirect() {
    if (window.location.search.length <= 1) {
    alert("You need a username and password")
    window.location="password_page_1.html"
    }
    }
    //
    // That works fine, but it will still redirect if there is an "?". I don't exactly want that...
    //
    function init() {
    
    var names = location.search.substring(1,location.search.length).split('&');
    var UserName = names[0].substring(names[0].lastIndexOf('=')+1,names[0].length);
    var Password = names[1].substring(names[1].lastIndexOf('=')+1,names[1].length);
    
    document.getElementById("UserName").value = UserName;
    document.getElementById("Password").value = Password;
    }
    
    function verify() {
    
    var UserName = document.getElementById("UserName");
    var Password = document.getElementById("Password");
    
    if (UserName.value=="WD") {
    if (Password.value=="JavaScript") {
    alert("Correct!")
    } else {
    alert("Invalid Password")
    window.location="password_page_1.html"
    }
    } else {
    alert("Invalid Username")
    window.location="password_page_1.html"
    }
    }
    </script>
    <form>
    <input type=text name=UserName class=hidden id=UserName>
    <input type=text name=Password class=hidden id=Password>
    </form>
    </body>
    The problem is that it won't do anything if the URL is, say, "?hi" but will if it's "?" or "?username=&password=".
    What do I do?
    What I want it to do is redirect unless there is "?username=WD&password=JavaScript" at the end of the URL.

  4. #4
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    You really shouldn't use javascript for any kind of validation. It's insecure and easily bypassed (just disable javascript). You should really look into server-side validation. If that's not an option at all, and you are sure you wanna use javascript validation (I highly recommend against it), I will still help out.

  5. #5
    Join Date
    Jan 2008
    Posts
    26
    Yes, I know that. I was just creating this script for fun...

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    Try:
    PHP Code:
    if (/BACKSLASH?usernameBACKSLASH=WDBACKSLASH&password=Javascript/.test(window.location)) {
    window.location "";

    Or if you just want, username=ANYTHING&password=SOMETHINGELSE:
    PHP Code:
    if (/BACKSLASH?usernameBACKSLASH=((BACKSLASHw|BACKSLASHd)+)BACKSLASH&password=((BACKSLASHw|BACKSLASHd)+)/.test(window.location)) {
    window.location "";

    EDIT: replace BACKSLASH with a back-slash \.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles