populate email field from url variable Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of populate email field from url variable without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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>
We are here to answer your question about populate email field from url variable - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji