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!

Answer

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>

Leave a Reply

Your email address will not be published. Required fields are marked *