[英]populate email field from url variable
I trying populate email field on a form from the url variable Ex: https://my.site.org/site/SignupPage.html?email=jsavage980%40gmail.com and it works for the most part except it is not pulling in the "@" from the email address. 我尝试从url变量Ex填充表单上的电子邮件字段: https : //my.site.org/site/SignupPage.html? email = jsavage980% 40gmail.com ,它在大多数情况下都起作用,除了它不会拉入电子邮件地址中的“ @”。 Here is my code:
这是我的代码:
<script type="text/javascript">
Y.use('jquery-noconflict', function() {
function getQuerystring(key, default_) {
if (default_ == null) default_ = "";
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null)
return default_;
else
return qs[1];
}
var other_value = getQuerystring('email');
other_value = other_value.replace(/(%[a-zA-Z0-9].)/g, "");
other_value = other_value.replace(/(\+)/g, "");
jQuery('#primary_email').val(other_value);
});
</script>
not sure what I am missing here... been racking my brain all morning. Any help would be greatly appreciated! Thanks!
First you will have to decode your URL correctly: 首先,您必须正确解码URL:
var url = decodeURIComponent(this.location);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
document.getElementById('output').textContent = decodeURIComponent(document.getElementById('input').textContent);
<div id="input">https://my.site.org/site/SignupPage.html?email=jsavage980%40gmail.com</div> <div id="output"></div>
I'm using this small jQuery plugin to read and decode query parameters: 我正在使用这个小jQuery插件来读取和解码查询参数:
/*
* jQuery plugin to get parse the URI query parameters.
* E.g. ?a=123&b=567 -> get parameters with $.uriQuery["a"] and $.uriQuery["b"]
*/
(function($)
{
$.uriQuery = (function()
{
var a = window.location.search.substr(1).split('&');
var b = {};
if (a!="")
{
for (var i=0; i<a.length; ++i)
{
var p = a[i].split('=');
if (p.length!=2)
continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
}
var i = window.location.hash.indexOf('?');
if (i>=0)
{
a = window.location.hash.substr(i + 1).split('&');
if (a!="")
{
for (var i=0; i<a.length; ++i)
{
var p = a[i].split('=');
if (p.length!=2)
continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
}
}
return b;
})()
})(jQuery);
The part starting from var i = window.location.hash.indexOf('?');
从
var i = window.location.hash.indexOf('?');
开始的部分var i = window.location.hash.indexOf('?');
is not required for your case, I need this to get query parameters from URIs in angularjs programs (the angularjs router adds #/...
to the URIs). 对于您的情况不是必需的,我需要这样做才能从angularjs程序中的URI获取查询参数(angularjs路由器在URI中添加
#/...
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.