更改按钮单击时请求的URL的参数 [英] Change parameter of the URL requested on a button click

查看:82
本文介绍了更改按钮单击时请求的URL的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提前道歉是因为无法提供更多信息,如果使用有限的信息无法做到这一点,我会删除这个问题。

I apologize in advance for not being able to supply more information, if this is not possible with the limited info, I will delete the question.

所以,我有一个脚本,生成一种依赖于脚本(也是动态生成)的搜索引擎,这非常复杂,我对它没有任何影响。

So, I have a script that generates kind of a search engine that depends on the script (also dinamically generated) which is quite complicated and I don't have any impact on it.

当您单击应用(搜索)按钮时,将触发搜索过程,您可以在具有一些参数的新URL上查看结果。

When you click on the apply (search) button, the search process is triggered and you can see the results on the new URL that has a few parameters.

E. g.: http://www..example.com/...&ThisParameter=XYZ&SecondParameter=foo&ThirdParameter=bar

我的问题是 - 是否可以将ThisParameter = XYZ更改为其他内容,例如: G。 ThisParameter =东西

My question is - is it possible to change "ThisParameter=XYZ" to something else, e. g. "ThisParameter=Something".

问题是我不知道如何构造URL并且不会影响它,所以我需要一个代码,它只会生成任何URL并生成更改此部分。

The problem is that I don't know how the URL is constructed and can'i impact it, so I would need a code that would just take any URL generated and change this part mentioned.

提前感谢您的意见!

编辑

我发现脚本中的一部分会影响生成的URL。当我在DOM中更改它时,它会将我重定向到所需的页面。

I have found the part of the script that impacts the URL generated. When I change it in DOM, it does redirect me to the desired page.

var options = {"FOO":123,"BAR":"ABC","ThisParameter":"STH","EXAMPLE":123, ...}

这是您选择复制路径时的输出:

And this is the output when you choose "copy path":

_options[0].ThisParameter


推荐答案

如果查询参数的顺序总是相同的:

IF THE SEQUENCE OF QUERY PARAMETERS ARE ALWAYS THE SAME:

var url = window.location.href;
var lst_temp = url.split('?');
var $lst_parts = lst_temp[1].split('&');

var new_val = 'blabla';
var position = 'x';
lst_parts[x] = 'ThisParameter=' + new_val;

var url_new = lst_temp[0] + '?';
url_new += lst_parts.join('&');

编辑:

如果在手之前不知道查询钥匙的订购这个:


IF ORDERING OF QUERY KEYS ARE NOT KNOWN BEFORE HAND, DO THIS:

从这里使用此功能: https://stackoverflow.com / a / 111545/888177

function EncodeQueryData(arr_data) {
   var ret = [];
   for (var d in arr_data)
      ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(arr_data[d]));
   return ret.join("&");
}

然后读取网址并将其拆分为:

Then read the url and split it like this:

var url = window.location.href;
var key = "ThisParameter";
var new_val = 'blabla';
var lst_temp = url.split('?');
var lst_parts = lst_temp[1].split('&');   

添加此循环以创建包含查询参数和值的关联数组:

Add this loop to create an associative array containing the query parameters and values:

var arr_data = new Array();
for (var index in lst_parts) {
   lst = lst_parts[index].split('=');
   arr_data[lst[0]] = lst[1];
}

为了避免无休止的刷新:

To avoid endless refresh:

var val = arr_data[key];
if (val != new_val) {
  arr_data[key] = new_val;
  var  new_url = lst_temp[0] + '?' + EncodeQueryData(arr_data);
  window.location.href = new_url;
}

这篇关于更改按钮单击时请求的URL的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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