Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes

I want to redirect the page to another page on an event with javascript.

On click of button page should be redirected using javascript from Index1 to Index2 .

I want 'Relative url redirect' to happen, how is this possible?

something like this:

<button onclick="document.location.href='/Index.aspx'"></button>

by (930 points)  

3 Answers

0 votes

URL redirection, likewise called URL sending, is an approach to naturally divert a website page to another site page. The diverted page is regularly on a similar site, or it very well may be on an alternate site or a web server. 

In Javascript, you can utilize numerous techniques to divert a page to another. Practically all strategies are identified with window.location object, which is a property of the Window object. It tends to be utilized to get the present URL address (web address) and to divert the program to another page.

Try followings:

You can try relative redirect using following any of the options:

window.location.href = "page2.php";

window.location.href = "";


<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

Relative Redirect Script


    function relatiiveredirect()





<button onclick="relatiiveredirect()"></button>

by (930 points)  
edited by
0 votes

You can redirect user by using document.location

document.location = "SomePage.html";

There are absolute URLS or relative paths you can redirect to.

absolute URLS

relative paths


You can remove redirectory from history by using replace() function

document.location.replace( "newPage.html" );

To redirect to https instead of http which is secure, try replace(): 

var a= document.URL.replace(/^http:/,"https:");

location.replace( a);

You can redirect with variables passing to the page:


by (930 points)  
0 votes

Some of javascript redirect to relative url methods:

//Redirect to Home Page on durrent directory or sub directory

   document.location = "/"  ; 

//Reidrect to different page in root:

   document.location = "/Main.html"; 

//Redirecting to  landing page on present directory

   document.location = "./" ; 

// Redirect to other page in same directory

   document.location = "./MainPage.html"; 

   document.location = "MainPage.html"  ; 

// Redirect to one directory up than landing page

   document.location = "../"; // 

//Redirect to another page, up one directory

   document.location = "../MainPage.html"; 

by (930 points)  
edited by