how to redirect a page using javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    #1

    how to redirect a page using javascript?

    hai all,
    i am having 2 pages. login.html and city.html

    login.html


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
        <script language="javascript" type="text/javascript">
        function validateuser()
        {
            var user= document.getElementById("Text1").value;
            if (user=="admin")
            {
                window.location="city.html?name="+user;
            }
            else
            {
                alert("Login Failed");
                document.getElementById("Text1").value="";
                document.getElementById("Text1").focus();
            }
        }
        
        </script>
    </head>
    <body>
        <input id="Text1" type="text" />&nbsp;
        <input id="Button1" type="button" value="LOGIN" onclick="validateuser()"/>
    
    </body>
    </html>
    City.html

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>CITY Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
        <link href="Styles/EIMS_blue.css" rel="stylesheet" type="text/css"/>
        <script language="javascript" type="text/javascript">
        function getsession()
        {
            var sessionvalue=document.getElementById("sessiondemo").value;
        }
        getvalues();
        function getvalues()
        {
            document.getElementById("username").value="";
            var parameterArray =unescape(self.location.search).substring(1).split( "&");
            var param=parameterArray[0];
            var param1=new Array();
            param1 = param.split('=');
    
            document.getElementById("username").value=param1[1];
            if(document.getElementById("username").value=="undefined")
            {
            window.location="login.html";
            }
            
    }
        
        </script>
    </head>
    <body>
        <script type="text/javascript" src="Scripts/Loaders_Collection.js"></script>
        <input type="text" id="username" />
        <table style ="width:100%; height:100%;" border="0" class="page_bg">
        
            <tr style ="width: 100%;">
                <td style="width:100%;height:126px">Header Area Sample</td>
            </tr>
            <tr style ="width:100%;">
                <td style="width:100%;">
                    <table style="width:100%; height:100%;" border="0">
                        <tr style ="width: 100%;">
                            <td>Menu Area Sample</td>
                            <td>
                               
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style ="width:100%;">
                <td style="width:100%;">Footer Area</td>
            </tr>
        </table>
    </body>
    </html>
    My problem is while redirecting page in city.html to login.html(when user name not given) the city.html page is shown for a fraction of second. how can i avoid this.

    Thanx in advance
    Last edited by acoder; Mar 27 '12, 05:29 PM.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can redirect ok. To solve your problem, you'll need to check using a server-side language. Using just javascript, you won't be able to avoid this.

    In any case, you shouldn't be using javascript for login. That should be done strictly server-side.

    Comment

    • omtech
      New Member
      • May 2013
      • 1

      #3
      Advanced JavaScript Redirection

      I think you only need this javascript redirect generator tool to solve your problem.
      Last edited by Frinavale; May 30 '13, 02:45 PM.

      Comment

      Working...