How to Remove Query String from URL in CodeIgniter using jQuery

How to Remove Query String from URL in CodeIgniter using jQuery

In this example we will show how to remove query string from url in codeigniter using jquery. I explain step by step to you remove query string from url. I will remove query string from url using jquery. After read this post you can easily remove codeigniter query string from url using jquery.

In this example we provide jquery remove query string from url in codeigniter. so let’s go following this example.

Read Also :  Click Button Copy to Clipboard using jQuery

Example : Remove Query String from URL in CodeIgniter

Step 1 : Create controller

In this step we will create Welcome controller for remove query sting from url. so let’s following code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function demo(){
        $_SESSION['data'] = $_GET;
        
        $this->load->view("welcome");
    }
}

Step 2 : Create view

In this step we will create welcome view and display query string data.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>how to remove query string from url in codeigniter</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <?= $this->session->userdata('name'); ?>
    <br>
    <?= $this->session->userdata('pwd'); ?>
</div>

</body>
<script>
    $(document).ready(function(){
        var uri = window.location.toString();
        if (uri.indexOf("?") > 0) {
            var clean_uri = uri.substring(0, uri.indexOf("?"));
            window.history.replaceState({},null, clean_uri);
        }
    });

</script>
</html>

I hope you understand of remove query string from url using jquery and can help you..