Changing the URL in the address bar with JQuery is quite straight forward. The purpose of this technique is to resolve the user support for the "single page application" (i.e., all contents were loaded by JQuery).
I have modified the technique used by the following article in order to avoid the security error raised by the browser. It appends the "anchor" to the URL.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function ChangeUrl(page, url) {
if (typeof (history.pushState) != "undefined") {
var i = window.location.href.indexOf('#');
if (i > 0) {
url = window.location.href.substring(0, i) + url;
}
else {
url = window.location.href + url;
}
var obj = { Page: page, Url: url };
history.pushState(obj, obj.Page, obj.Url);
} else {
alert("Browser does not support HTML5.");
}
}
$(function () {
$("#button1").click(function () {
ChangeUrl('Page1', '#Page1');
});
$("#button2").click(function () {
ChangeUrl('Page2', '#Page2');
});
$("#button3").click(function () {
ChangeUrl('Page3', '#Page3');
});
});
</script>
<input type="button" value="Page1" id="button1" />
<input type="button" value="Page2" id="button2" />
<input type="button" value="Page3" id="button3" />
Reference
http://www.aspsnippets.com/Articles/Change-URL-in-Browser-Address-Bar-without-reloading-using-JavaScript-and-jQuery.aspx
Subscribe to:
Post Comments (Atom)
We are moving
We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm
-
Overview Usually, we start developing a new system with designing database and then writing codes. If you are not a fan of ORM, you will h...
-
You can draw a simple pie chart & doughnut chart with SVG + CSS. This is quite simple and straight forward. But the limitation is that i...
-
Summary of web worker Every web worker is a thread on it's own!! Instantiate too many worker instances might degrade your computer p...
No comments:
Post a Comment