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 8 of 8
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Redirect Driving Me Crazy Javascript

    This has been driving me crazy all day, cant seem to work out what im doing wrong.

    Alerts the correct URL
    Used document.write and it works
    But the redirect command doesnt initiate automatically on click and im not sure why.

    Someone help!!


    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>http</title>
    <style>
    <!--
     p.MsoNormal
    	{mso-style-parent:"";
    	margin-bottom:.0001pt;
    	font-size:11.0pt;
    	font-family:"Calibri","sans-serif";
    	margin-left:0cm; margin-right:0cm; margin-top:0cm}
    -->
    </style>
    
    <script type="text/javascript">
    
    function buildstring(form)
    {
    
    var text = form.text.value;
    var xmljson = form.choice1.value;
    var init = 'localhost:1234/PPTX/tr?service=extract&action=extractAll&text=';
    var op = '&op=';
    var first = init + text + op + xmljson;
    
    var complete = "http://" + first;
    
    alert (complete);
    
    window.location = complete;
    	
    }
    
    
    
    </script>
    </head>
    
    <body>
    
    <form method="get" id="urlbuild">
    
    <table>
    
    <tr>
    <td>
    <h2>URL Builder</h2>
    </td>
    </tr>
    
    <tr>
    <td>
    <input type="text" size="100" name="text" value="">
    </td>
    </tr>
    </table>
    
    <table>
    <tr>
    <td>
    <select type="one" name="choice1">
    <option value="xml"> XML
    </option>
    <option value="json"> JSON
    </option>
    </select>
    </td>
    
    
    <td>
    <p>Please choose XML or JSON.</p>
    </td>
    </tr>
    </table>
    
    <table>
    <tr>
    <td>
    <input type="submit" onClick="buildstring(this.form)"> 
    
    </td>
    </tr>
    </table>
    
    
    </form>
    
    </body>
    
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Try

    window.location.href = complete;
    return false;

    Or if that fails

    window.location.assign(complete);




    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-02-2012 at 05:19 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.

  • Users who have thanked Philip M for this post:

    GStatus (05-03-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Ummm...just for starters, try copy/pasting the url to your browser address line.

    Or, just for starters, try hitting this URL with your browser:
    Code:
    localhost:1234/PPTX/tr
    Unless "tr" there is a directory and/or unless you have some URL REWRITE rules in place, I can't help but wonder if you really have a page with the name.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    GStatus (05-03-2012)

  • #4
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for the help, sadly still doesnt work though :S

    Pendant, the URL is correct, im working off a server.

    Please see ammended attempt using google as the redirect page.

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>http</title>
    <style>
    <!--
     p.MsoNormal
    	{mso-style-parent:"";
    	margin-bottom:.0001pt;
    	font-size:11.0pt;
    	font-family:"Calibri","sans-serif";
    	margin-left:0cm; margin-right:0cm; margin-top:0cm}
    -->
    </style>
    
    <script type="text/javascript">
    
    function buildstring(form)
    {
    
    var text = form.text.value;
    var xmljson = form.choice1.value;
    var init = 'localhost:9999/TRWS/tr?service=extract&action=extractAll&text=';
    var op = '&op=';
    var first = init + text + op + xmljson;
    
    var complete = "http://" + first;
    var complete2 = "http://www.google.co.uk";
    
    alert (complete);
    alert (complete2);
    
    window.location.assign(complete2);
    
    }
    
    
    
    </script>
    </head>
    
    <body>
    
    <form method="get" id="urlbuild">
    
    <table>
    
    <tr>
    <td>
    <h2>URL Builder</h2>
    </td>
    </tr>
    
    <tr>
    <td>
    <input type="text" size="100" name="text" value="">
    </td>
    </tr>
    </table>
    
    <table>
    <tr>
    <td>
    <select type="one" name="choice1">
    <option value="xml"> XML
    </option>
    <option value="json"> JSON
    </option>
    </select>
    </td>
    
    
    <td>
    <p>Please choose XML or JSON.</p>
    </td>
    </tr>
    </table>
    
    <table>
    <tr>
    <td>
    <input type="submit" onClick="buildstring(this.form)"> 
    
    </td>
    </tr>
    </table>
    
    
    </form>
    
    </body>
    
    </html>

  • #5
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    i even created a new function and still wont process it onclick

    Code:
    function redirect()
    {
    
    var complete2 = "http://www.google.co.uk";
    window.location.href = complete2;
    return false;
    }
    
    <input type="submit" onClick="redirect()">
    May it have something to do with the form method?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Your button must be within a form, and must be type "button", not "submit". When a visitor clicks a submit button, the form is sent to the address specified in the action setting of the <form> tag. You have specified no such action.


    Code:
    <script type = "text/javascript">
    
    function redirect(){
    var complete2 = "http://www.google.co.uk";
    window.location.href = complete2;
    return false;
    }
    
    </script>
    
    <form>
    <input type="button" value = "Redirect" onClick="redirect()">
    </form>
    Last edited by Philip M; 05-03-2012 at 10:10 AM.

    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.

  • Users who have thanked Philip M for this post:

    GStatus (05-03-2012)

  • #7
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thanks Phil your a life saver, the button was in the form i just lifted it to show the onclick change.

    The issue was that the input type was a submit and not a button!!!!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by GStatus View Post
    thanks Phil your a life saver, the button was in the form i just lifted it to show the onclick change.

    The issue was that the input type was a submit and not a button!!!!
    Yes, quite so. Sorry that I did not spot that basic mistake earlier, but your question as posed was slightly misleading.

    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.


  •  

    Posting Permissions

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