简体   繁体   中英

Javascript - Remove query string from current url

I need to remove query string value from the url once submit button is clicked. Can i able to do this with jquery?

Current Url:

siteUrl/page.php?key=value

After Page submit:

siteUrl/page.php

Actually i have landing to current page from another one with query string. I need query string value when page loads first time to prefill some details. But once i submitted the form, i need to remove query string value.

I have tried like this.

$('#submit').click(function(){
var newUrl = window.location.href.replace(window.location.search,'');
window.location.href  = newUrl;
return false;
});

It makes changes in url as expected. but cant able to get the posted values.

Thanks in advance :)

How about this one. Hope it helps :)

$('#myform').submit(function(event){
event.preventDefault();
var currentURL = window.location.href ;
location.href = currentURL.substring(0, currentURL.indexOf('?'));
});

index.html

<form id = "myform">
<input type = "text">
<input type = "submit" id = "submit" value = "Send">
</form>
function getQueryString(url) {
  return url.split("?")[0];
}
var Url=getQueryString("siteUrl/page.php?key=value");
console.log(Url);

You may try this for getting url

Unfortunately i cant able to do it with javascript or jquery. So i go through with php redirection, now it works.

<?php
if(isset($_POST['Submit'])) {
    // actions
    if(isset($_REQUEST['key']) && ($_REQUEST['key'] != "")) {
        header('Refresh: 1;url='.$_SERVER['PHP_SELF']);
    }
}
?>

Thanks all :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM