• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
191 views

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 = "http://www.kodlogs.com";

HTML

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

Relative Redirect Script

<script>

    function relatiiveredirect()

    {

        window.location.href='/Main2.aspx';

    }

</script>

<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

https://kodlogs.com

relative paths

 ("../subdirectiory/IndexPage.html").

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:

document.getElementById("Form2").submit();

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
...