How can JavaScript be utilized to handle redirection in conjunction with PHP scripts for a seamless user experience?

When using PHP scripts to handle form submissions or other actions that require redirection, JavaScript can be utilized to handle the redirection on the client-side for a seamless user experience. This can be achieved by sending a response from the PHP script to the client with the URL to redirect to, and then using JavaScript to redirect the user to that URL.

<?php
// Process form submission or other action
// Redirect to a new page after processing
header("Content-Type: application/json");
echo json_encode(array("redirect" => "new_page.php"));
exit;
?>
```

```javascript
// JavaScript code to handle redirection
fetch('process.php', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.then(data => {
  if(data.redirect) {
    window.location.href = data.redirect;
  }
});