I have read a lot of other questions on how to change the URL with out reloading the page. This is my code that I have just wondering if someone can tell me what I am doing wrong? When I click the button it doesn't change the url. This is my exact code and not sure what I have done wrong, Thanks.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Social Media </title>
</head>
<body>
<input type="button" value="Page1" onclick="ChangeUrl('Page1','Page1.htm');" />
<script type="text/javascript">
function ChangeUrl(title, url) {
window.history.pushState("object or string", "Title", "/new-url");
}
</script>
</body>
</html>
I want the new url to have /new-url onto the end. Thanks
Dan PaullDan Paull
-
1What error are you getting? What about it isn't working? What browser are you testing it in?Karl-Johan Sjögren– Karl-Johan Sjögren2015年03月24日 11:04:24 +00:00Commented Mar 24, 2015 at 11:04
-
This might help you. stackoverflow.com/questions/10445229/…Pavan Bhushan Maganti– Pavan Bhushan Maganti2015年03月24日 11:07:05 +00:00Commented Mar 24, 2015 at 11:07
-
2possible duplicate of Modify the URL without reloading the pagePugazh– Pugazh2015年03月24日 11:07:19 +00:00Commented Mar 24, 2015 at 11:07
-
this could help you too.. stackoverflow.com/questions/6068891/…lateralus– lateralus2015年03月24日 11:15:23 +00:00Commented Mar 24, 2015 at 11:15
-
What's the problem or issue that you are having ? Let us know that .The Dark Knight– The Dark Knight2015年03月24日 11:28:50 +00:00Commented Mar 24, 2015 at 11:28
2 Answers 2
<input type="button" value="Page1" onclick="ChangeUrl('Page1','Page1.htm');" />
<script type="text/javascript">
function ChangeUrl(title, url)
{
window.history.pushState("object or string", title, url);
}
</script>
answered Mar 24, 2015 at 11:25
Sign up to request clarification or add additional context in comments.
1 Comment
Jay Blanchard
Why should the OP try this? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO.
You can do this by two ways.
pushStatecreates one new history entryreplaceStateupdates the current history entry
You can do something like this:
function changeURL(title, url)
{
history.replaceState("", title, url);
}
A.L
10.6k10 gold badges73 silver badges106 bronze badges
answered Mar 24, 2015 at 12:17
1 Comment
Dan Paull
I am trying to use the pushState, but I can't get it to work, just wondering if you knew what i did wrong, thanks.
default