简体   繁体   中英

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. 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:

var url = decodeURIComponent(this.location);

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 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('?'); 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM