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

查看:32
本文介绍了将参数附加到当前 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.

推荐答案

试试这个代码,

var separator = (window.location.href.indexOf("?")===-1)?"?":"&";
window.location.href = window.location.href + separator + "ts=true";

为避免 url 中出现重复参数或非常大的字符串,您需要替换它已经存在的旧参数.

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天全站免登陆