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

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

问题描述

如果在&放大器;查看,所有参数不会在我需要将它与值一起添加到URL末尾的URL存在。如果确实存在那么我需要能够只需更改值而无需创建一个新的URL,因为它可能会,也可能不会,之前有其他的参数。

我发现这个功能,但我不能得到它的工作: http://stackoverflow.com/a/10997390/837705

下面是code我已经用上面的功能(我不能去上班): HTTP: //jsfiddle.net/Draven/tTPYL/4/

我知道如何在参数和值已追加:

< D​​IV的onclick =JavaScript的:window.location.assign(window.location.href + ='和;查看,所有=是');>等等等等< / DIV>

更多信息:

如果URL是 http://www.domain.com/index.php?action=my_action 那么默认的&放大器;查看所有值是是的,这样的网址,他们将被定向到当他们点击按钮 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。


解决方案

 函数setGetParameter(paramName配置,paramValue)
{
    VAR URL = window.location.href;
    VAR哈希=的location.hash;
    URL = url.replace(哈希,'');
    如果(url.indexOf(paramName配置+=)GT; = 0)
    {
        VAR preFIX = url.substring(0,url.indexOf(paramName配置));
        变种后缀= url.substring(url.indexOf(paramName配置));
        后缀= suffix.substring(suffix.indexOf(=)+ 1);
        后缀=(suffix.indexOf(与&)> = 0)? suffix.substring(suffix.indexOf(与&)):,;
        URL = preFIX + paramName配置+=+ paramValue +后缀;
    }
    其他
    {
    如果(url.indexOf()&所述?; 0)
        网址+ =? + paramName配置+=+ paramValue;
    其他
        网址+ =&放大器; + paramName配置+=+ paramValue;
    }
    window.location.href =网址+散;
}

调用函数上面的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: http://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.

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

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