7

I have this url http://localhost:1234/pag1

I want to be like below when I click on the div:

 http://localhost:1234/pag1?fare=standar

and I have an onclick event function:

 <div id="" onclick="addUrl();"> </div>

And the js code:

<script type="text/javascript">
 function addUrl() {
 var url = window.location.href;
 window.location.hash = '?fare=standar';
};
 </script>

This give me this result:

http://localhost:1234/pag1#?fare=standar

And I don't Know how to remove the (#). I only need to add the parameter to the url, without refresh the page.

Suggest me! Thank's.

asked Nov 20, 2015 at 9:41
3
  • on clicking the div do you wnat to reload the page with the new url? Commented Nov 20, 2015 at 9:44
  • On clicking the div I want tho add the new parameter to the actual url, without reload the page. Commented Nov 20, 2015 at 9:45
  • then you need to strore the url in a variable. You can not show it in url. Commented Nov 20, 2015 at 9:48

5 Answers 5

15

Use window.history.pushState( {} , '', '?fare=standar' );

This should update the url without refresh.

You can also do a bit of reseach on pushState as the implementation can differ.

answered Nov 20, 2015 at 9:52
Sign up to request clarification or add additional context in comments.

3 Comments

Just tested that method on stackoferflow page and it works. Type the method in console and see the result
Please mark the answer if it's the correct one :) Thanks
Yes, your answer works correctly, thank's for your help.
1

window.location.hash always put "#" in url

you can use to push paramter in url by using this function

function setGetParameter(paramName, paramValue)
{
 var url = window.location.href;
 if (url.indexOf(paramName + "=") >= 0)
 {
 var prefix = url.substring(0, url.indexOf(paramName));
 var suffix = url.substring(url.indexOf(paramName));
 suffix = suffix.substring(suffix.indexOf("=") + 1);
 suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
 url = prefix + paramName + "=" + paramValue + suffix;
 }
 else
 {
 if (url.indexOf("?") < 0)
 url += "?" + paramName + "=" + paramValue;
 else
 url += "&" + paramName + "=" + paramValue;
 }
 window.location.href = url;
}

i hope this will helpful to you

answered Nov 20, 2015 at 9:54

1 Comment

Thank's for your answer, but is too long code, I get the solution in one line ;-D
1

Try this

HTML

<div onclick="addUrl()"> </div>

Javascript

 <script type="text/javascript">
 function addUrl() {
 var url = window.history.pushState( {} , '', '?fare=standar' );
 </script>

Quick Explanation : When user clicks on div, URL will update without refreshing the page.

Hope this will work for you, thank you :-).

answered Apr 5, 2020 at 17:00

Comments

-2

Try this

var myURL = document.location;
document.location = myURL + "?a=parameter";

Or

location.hash = 'a=parameter'

Or without reloading the page

window.history.pushState("object or string", "Title", "new url");

Or without reloading the page

window.history.replaceState(null, null, "/another-new-url");
answered Nov 20, 2015 at 9:50

1 Comment

I don't want to reload the page.
-3

Why dont you use jQuery? I prepared sample like here in JsFiddle:

$("#clickableDiv").click(function(){
 var url = "http://localhost:1234/pag1";
 var parameter = "fare=standar";
 $("#AppendUrl").text(url+ "?" + parameter); 
});
Bartłomiej Semańczyk
61.9k52 gold badges255 silver badges407 bronze badges
answered Nov 20, 2015 at 9:51

2 Comments

Thank you for your answer, but you add the url to a <p>, I need to add this to URL without reload the page.
I don't understand you. Did you mean Ajax ? Something like this : stackoverflow.com/questions/18490026/…

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.