Home > Blockchain >  Page switching without reload
Page switching without reload

Time:10-08

I have a Website with 2 pages and therefore I have 2 buttons to switch the page. I want to change the Website only if it´s needed, so for example If I´m using page 1 and I click the button for page 1 it shouldn´t reload.

The first step I guess is to check the URL $link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

but with research i couldn´t go further :/ Has anyone a idea how I can do that - it´s not important for me to use tag.

CodePudding user response:

You can do something like this :

<button onclick="switchPage(this)" data-page="index.php">page 1</button>
<button onclick="switchPage(this)" data-page="test.php">page 2</button>

In order to redirect to the desired page, you can simply check the url and the button pressed using javascript or jquery.

Here is an example using javascript :

function switchPage(page) {
        let pageName = page.dataset.page;
        let path = location.href.split("/").slice(-1); 
        if(pageName != path) {
            window.location.href=pageName;
        }
    }
  • Related