最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can Re-writeModify URL with javascriptJquery after page load - Stack Overflow

programmeradmin2浏览0评论

Is it possible to re-write URL without reloading the page via jQuery/Javascript?

Let's say you have the following URL:

can we append the value: /pId=XYZ

So you can get

Is this possible?

Is it possible to re-write URL without reloading the page via jQuery/Javascript?

Let's say you have the following URL:

http://stackoverflow./questions/ask

can we append the value: /pId=XYZ

So you can get

http://stackoverflow./questions/ask/pId=XYZ

Is this possible?

Share Improve this question edited Jun 14, 2017 at 8:49 Malcolm Salvador 1,5663 gold badges22 silver badges41 bronze badges asked Aug 17, 2012 at 13:08 BJ PatelBJ Patel 6,27811 gold badges50 silver badges84 bronze badges 1
  • It's very easy to do a proper formatting of your question, maybe you could do it? – Olivier Pons Commented Aug 17, 2012 at 13:59
Add a ment  | 

5 Answers 5

Reset to default 6

All you can change without redirecting is the hash part of url:

document.location.hash = "whatever";

Yes you can, and you don't need jquery : simply use the History API : https://developer.mozilla/en-US/docs/DOM/Manipulating_the_browser_history

Beware that it's usually a little painful to handle history in a plex ajax application, as you have to handle state serialization of the pages, state loading, and so on. And you can't apply this to a badly designed site as are many ajaxified sites. And this won't work on "old" browsers like IE9.

Yes we can, but only on browsers implementing pushState.

See other answers here

you can get the current Url with

var url = document.location.href;

then you add the part to the url

url += "/mySubFolder";

rest of my post didnt fit the question ... i should read more carefully :( !

After encountering this problem, I think the @DenysSéguret answer is exactly what you need.

When it's supported by the browser, you can use

window.history.pushState(stateObj, title, url);

Or

window.history.replaceState(stateObj, title, url);

As stated by @DenysSéguret, you can find the documentation at
https://developer.mozilla/en-US/docs/Web/API/History_API

发布评论

评论列表(0)

  1. 暂无评论