添加网址的/更改参数,并重定向到新网址

编程入门 行业动态 更新时间:2024-10-27 04:30:44
本文介绍了添加网址的/更改参数,并重定向到新网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

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

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

更多信息:

如果URL是 www.domain/index.php?action=my_action 那么默认的&放大器;查看所有值是是的,这样的网址,他们将被定向到当他们点击按钮 www.domain/index.php?action=my_action&view-all=Yes 搜索结果

如果URL是 www.domain/index.php?action=my_action&view-all=Yes 然后当他们点击按钮,它将改为 www.domain/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: stackoverflow/a/10997390/837705

Here is the code I have using the function above (which I can't get to work): jsfiddle/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 www.domain/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 www.domain/index.php?action=my_action&view-all=Yes.

If the URL is www.domain/index.php?action=my_action&view-all=Yes then when they click the button it would change to www.domain/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.

更多推荐

添加网址的/更改参数,并重定向到新网址

本文发布于:2023-10-31 18:30:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   到新   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!