-1

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 Paull
  • 19
  • 4

2 Answers2

0
<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>
  • 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. – Jay Blanchard Mar 24 '15 at 14:03
0

You can do this by two ways.

  1. pushState creates one new history entry
  2. replaceState updates the current history entry

You can do something like this:

function changeURL(title, url)
{
    history.replaceState("", title, url);
}
A.L
  • 10,259
  • 10
  • 67
  • 98
Abdul Alim
  • 346
  • 3
  • 15
  • 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. – Dan Paull Mar 30 '15 at 02:26