I'd like to have a little easter egg button in the corner of a project site. When clicked, it just drops a string onto the current url and reloads the page.
So if I'm on: http://test.com/projects/view/134
The button is clicked
Page reload on: http://test.com/projects/view/134?ts=true
Not really sure how I might go about doing so though.
try this code,
var separator = (window.location.href.indexOf("?")===-1)?"?":"&";
window.location.href = window.location.href + separator + "ts=true";
EDITS: to avoid duplicated parameter or very large string in your url, you need to replace the old param it already exists.
var url=window.location.href,
separator = (url.indexOf("?")===-1)?"?":"&",
newParam=separator + "ts=true";
newUrl=url.replace(newParam,"");
newUrl+=newParam;
window.location.href =newUrl;
You can assign location.href to the value it currently has, plus your new querystring:
(edited to be friendly to existing querystrings)
$("#yourButtonId").click({
var loc = location.href;
if (loc.indexOf("?") === -1)
loc += "?";
else
loc += "&";
location.href = loc + "ts=true";
});
Or to be more succinct:
$("#yourButtonId").click({
var loc = location.href;
loc += loc.indexOf("?") === -1 ? "?" : "&";
location.href = loc + "ts=true";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With