附加一个参数到当前的URL [英] Append a param onto the current URL

查看:110
本文介绍了附加一个参数到当前的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个项目工地的一角有点复活节彩蛋按钮。单击时,它只是滴串到当前URL并重新加载页面。

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.

所以,如果我在:
http://test.com/projects/view/134

按钮被点击

页面重载的:
http://test.com/projects/view/134?ts=true

不是真的知道我怎么会去这样做虽然。

Not really sure how I might go about doing so though.

推荐答案

试试这个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;

这篇关于附加一个参数到当前的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆