WordPress.org

Ready to get started?Download WordPress

Forums

Javascript being naughty (4 posts)

  1. JerBurdett
    Member
    Posted 2 years ago #

    <script type="text/javascript">
    function redirect_to(){
    var pathname = window.location.host;
    var s = document.getElementById('redirect_s').value;
    if(s == "")
    {
    alert('Please Enter the number in textbox');
    }
    else{
    location.replace( "/" + s) ;
    }
    
    }
    </script>
    <form method="post" action="">
    <label class="assistive-text" for="s">Search</label>
    <input id="redirect_s" class="field" type="text" placeholder="Jump to image #" name="redirect_s">
    <input id="searchsubmit" class="submit" type="button" onclick="redirect_to();" value="Search" name="submit">
    </form>

    This bit of code should be directing users to onceuponasidewalk.com/whatevertheyenteredintextbox, but it has decided to instead direct them back to whatever page they started on.

    Any ideas what's going wrong here?

  2. JerBurdett
    Member
    Posted 2 years ago #

  3. Chandan Kumar
    Member
    Posted 2 years ago #

    I have made some change on your javascript code, please write your script code before end of head tag:

    function redirect_to(){
        var pathname = window.location.host;
        var s = document.getElementById('redirect_s').value;
        if(s == "")
        {
        alert('Please Enter the number in textbox');
        }
        else{
        location.replace( "/" + s) ;
        }
        return false;
      }

    And also made following changes on your html form:
    1. added form action
    2. removed onClick from form button

    Here's code:

    <form method="post" action="javascript:redirect_to();">
    <label class="assistive-text" for="s">Search</label>
    <input value="3" id="redirect_s" class="field" placeholder="Jump to image #" name="redirect_s" type="text">
    <input id="searchsubmit" class="submit"  value="Search" name="submit" type="submit">
    </form>

    Implement all the above mentioned changes.

  4. JerBurdett
    Member
    Posted 2 years ago #

    Basically, you rock. I paid some dude on fiverr to write that snippet for me and obviously it didn't turn out so well.

    This is the code I ended up with, in case someone else want to have a jump to page text box:

    <script type="text/javascript">
    function redirect_to(){
    var pathname = window.location.host;
    var s = document.getElementById('redirect_s').value;
    if(s == "")
    {
    alert('Please Enter the number in textbox');
    }
    else{
    location.replace( "/" + s) ;
    }
    
    }
    </script>
    <form method="post" action="javascript:redirect_to();">
    <label class="assistive-text" for="s">Search</label>
    <input value="" id="redirect_s" class="field" placeholder="Jump to image #" name="redirect_s" type="text">
    <input id="searchsubmit" class="submit"  value="Search" name="submit" type="submit">
    </form>

    Thanks again, Chandan!

Topic Closed

This topic has been closed to new replies.

About this Topic