-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

asked Mar 24, 2015 at 11:00
6

2 Answers 2

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>
answered Mar 24, 2015 at 11:25
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.6k10 gold badges73 silver badges106 bronze badges
answered Mar 24, 2015 at 12:17

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.