Javascript:使用当前URL中的变量来创建和打开新的URL
Javascript: Using variables from current URL to create and open new URL
我有一个当前的网址,如下所示:
http://example?variables1=xxxx&example&variables2=yyyyy
我想使用变量 1 和变量 2 创建一个新 URL 并打开这个新 URL:
http://example?variables3=variables1&example&variables4=variables2
我希望有人可以帮助我解决这个问题:)
您需要从第一个 URL 解析所需的查询参数,并使用字符串添加来创建第二个 URL。
您可以使用此代码从 URL 获取特定查询参数。 如果你使用它,你可以得到变量 1 和变量 2,如下所示:
var variables1 = getParameterByName("variables1");
var variables2 = getParameterByName("variables2");
然后,您可以使用它们来构建您的新 URL。
newURL = "http://example.com/?variables1=" +
encodeURIComponent(variables1) +
"&someOtherStuff=foo&variables2=" +
encodeURIComponent(variables2);
因为我不完全明白需要改变什么,这是我最好的尝试*,使用其他答案和在线资源的混搭。
// the original url
// will most likely be window.location.href
var original = "http://example?variables1=xxxx&example&variables2=yyyyy";
// the function to pull vals from the URL
var getParameterByName = function(name, uri) {
name = name.replace(/['[]/, "'''[").replace(/[']]/, "''']");
var regexS = "[''?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(uri);
if(results == null) return "";
else return decodeURIComponent(results[1].replace(/'+/g, " "));
};
// so, to get the vals from the URL
var variables1 = getParameterByName('variables1', original); // xxxxx
var variables2 = getParameterByName('variables2', original); // yyyyy
// then to construct the new URL
var newURL = "http://" + window.location.host;
newURL += "?" + "variables3=" + variables1;
newURL += "&example&"; // I don't know what this is ...
newURL += "variables4=" + variables2;
// the value should be something along the lines of
// http://example?variables3=xxxx&example&variables4=yyyy
*所有这些都未经测试。
相关文章:
- 创建按钮,根据表单字段中的数据获取特定的URL
- 如何使用特定的javascript函数创建一个url,以便在加载页面时运行
- 如何创建背景图像列表'URL,并使用jQuery在单击函数时应用它们
- 在任意字符串中创建所有URL的链接
- ExtJS:从受保护的url创建JSB3文件
- Angular ng重复创建图像-也尝试解析url
- 创建自定义URL
- 使用来自不同页面的cookie创建URL,并使用JS重定向
- 有条件地创建ajax url参数以与jQuery Validate一起使用
- 如何创建一个组件,我可以设置在 Angular 2 中的属性中获取 URL 数据
- 如何使用jqueryjplayer的视频url创建缩略图
- 使用自定义内容而不是url创建弹出窗口
- 从 URL 创建对象
- 从gwt中受密码保护的url创建图像对象
- 基于页面URL创建条件语句
- 如何从URL创建回传标记
- 从外部图像url创建Blob
- 如何从图像url创建Blob对象
- 根据URL创建变量
- 从 Blob URL 创建下载链接