添加/更改 URL 参数并重定向到新 URL [英] Add / Change parameter of URL and redirect to the new URL

查看:40
本文介绍了添加/更改 URL 参数并重定向到新 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 &view-all 参数在 URL 中不存在,我需要将它与一个值一起添加到 URL 的末尾.如果它确实存在,那么我需要能够在不创建新 URL 的情况下更改该值,因为它之前可能有也可能没有其他参数.

我找到了这个功能,但我无法让它工作:https://stackoverflow.com/a/10997390/837705

这是我使用上述函数的代码(我无法开始工作):http://jsfiddle.net/Draven/tTPYL/4/

我已经知道如何附加参数和值:

<div onclick="javascript: window.location.assign(window.location.href+='&view-all=Yes');">Blah Blah</div>

更多信息:

如果 URL 是 http://www.domain.com/index.php?action=my_action 那么默认的&view-all"值为Yes",所以 URL当他们点击按钮时,他们会被引导到 http://www.domain.com/index.php?action=my_action&view-all=Yes.

如果 URL 是 http://www.domain.com/index.php?action=my_action&view-all=Yes 那么当他们点击按钮时它会变成 http://www.domain.com/index.php?action=my_action&view-all=No

请举例说明.我对 JS 不是很了解,我只是想不出用 PHP 来做的方法.

解决方案

function setGetParameter(paramName, paramValue){var url = window.location.href;var hash = location.hash;url = url.replace(hash, '');如果 (url.indexOf(paramName + "=") >= 0){var prefix = url.substring(0, url.indexOf(paramName + "="));var 后缀 = url.substring(url.indexOf(paramName + "="));后缀 = suffix.substring(suffix.indexOf("=") + 1);后缀 = (suffix.indexOf("&") >= 0) ?suffix.substring(suffix.indexOf("&")) : "";url = 前缀 + paramName + "=" + paramValue + 后缀;}别的{如果(url.indexOf(?")<0)网址+=?"+ paramName + "=" + paramValue;别的网址 += "&"+ paramName + "=" + paramValue;}window.location.href = url + 哈希;}

在您的 onclick 事件中调用上面的函数.

If the &view-all parameter does NOT exist in the URL I need to add it to the end of the URL along with a value. If it DOES exist then I need to be able to just change the value without creating a new URL because it might, or might not, have other parameters before it.

I found this function but I can't get it to work: https://stackoverflow.com/a/10997390/837705

Here is the code I have using the function above (which I can't get to work): http://jsfiddle.net/Draven/tTPYL/4/

I know how to append the parameter and value already:

<div onclick="javascript: window.location.assign(window.location.href+='&view-all=Yes');">Blah Blah</div>

More Info:

If the URL is http://www.domain.com/index.php?action=my_action then the default "&view-all" value would be "Yes" so the URL they would be directed to when they click the button is http://www.domain.com/index.php?action=my_action&view-all=Yes.

If the URL is http://www.domain.com/index.php?action=my_action&view-all=Yes then when they click the button it would change to http://www.domain.com/index.php?action=my_action&view-all=No​​​

EDIT: Please give me examples. I don't know alot of JS, and I just can't think of a way to do it in PHP.

解决方案

function setGetParameter(paramName, paramValue)
{
    var url = window.location.href;
    var hash = location.hash;
    url = url.replace(hash, '');
    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 + hash;
}

Call the function above in your onclick event.

这篇关于添加/更改 URL 参数并重定向到新 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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