How can I remove the query string from the url after the page loads?

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I have a URL with a long query string attached to it. After the page loads, I do not require the query string. So I want to remove the query string from the address bar without a page reload.

I tried parent.location.hash = ''; and window.location.href = '/#'

They did not make a difference.

回答1:

You can't do that without reloading the page, imagine if you could put whatever you wanted in the browser address bar? Security fun :)

Although you can now do it in HTML5 (which will only work on browsers supporting it) using the new history API, but realistically, your scenario best warrants a rewrite instead of including that (seems sledgehammer to crack a nut).

As you said you don't need the query string after the page loads, you should really post back, then redirected to another URL after you've finished your processing.



回答2:

As others have said, you can do this using the History API in modern browsers (IE10+, FF4+, Chrome5+). There was no full example in the answers, so figured I'd share my solution, as I just had a requirement to do the same thing:

history.pushState(null, "", location.href.split("?")[0]); 

If you are using Modernizr, you can also check if the History API is available like so:

if (Modernizr.history) {     history.pushState(null, "", location.href.split("?")[0]); } 


回答3:

That is achievable in modern browsers using the history api, but probably isn't the best solution to your problem.

history.replaceState({}, 'some title', '/'); 

It sounds like you would be better off processing the data and then redirecting to the homepage instead of returning an HTML document directly.

Since you aren't wanting to keep the URL around, it won't be useful for bookmarking, so it is quite likely you would be better off making a POST request.

This suggests that you should be using the POST-Redirect-GET pattern.



回答4:

Use history.replaceState({}, "Title", "page.html");

same syntax for pushState. However you will have to find a way to make IE understand that.

A better way would be to use Apache mod_rewrite module. It's quite easy to use.



回答5:

Updated Code Now it will work

Just Add Below code in you page whose link you want to change.

// javascript function     function buildLin(first) {     var firs = first.toString()        //alert(firs);        //document.getElementById("team").value = "1";            document.location.hash = "#" + firs.replace(/ /g, "_");         //alert(document.location.hash);     }   //jQuery to call above function after page loads    $(document).ready(function () {       buildLin(2);   }); 

Don't forget to add http://code.jquery.com/jquery-latest.js



回答6:

I have faced the same problem. My solution was these: Get the request like this:

operation.aspx?opr=lock 

After finishing the operation redirect like this:

Response.Redirect("operation.aspx"); 

In the response, if there is any message, I print them like this:

if (Session["InfoMessages"] != null) {     lblMessage.Text = (string)Session["InfoMessages"];     Session["InfoMessages"] = null; } 

I hope it will help others :)



回答7:

I use this code snippit in my personal projects where i need to remove URL params without re-loading:

var newURL = location.href.split("?")[0]; window.history.pushState('object', document.title, newURL); 


回答8:

You could avoid a query string altogether by using POST instead of GET.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!