0

Hi I have a link on my home page:

<a href="contact.html?id=Graphic Design#section1" class="btn btn-info">Get a Quote</a>

This link will take you to the contact page. I use this script below to autofill the subject field in my form with the id taken from the URL.

<script type="text/javascript">
window.onload=function(){ 
function querySt(ji) {
hu = window.location.search.substring(1); 
gy = hu.split("&");
for (i=0;i<gy.length;i++) { 
ft = gy[i].split("=");  if (ft[0] == ji) { return ft[1];  } }} 
var id = querySt("id");
if( id==null){ 
}else{ 
document.getElementById('form_subject').value = id; } } 
</script>

Because there is a space in the URL id (Graphic Design) it ends up in the form field like this: Graphic%20Design

I have read many post here that suggest how to remove the %20 but I have not been successful in implementing that. I'm not sure how or where to place code.

Any help is greatly appreciated. Thank you.

user2765741
  • 49
  • 2
  • 9
  • there are multiple ways to accomplish this task; Instead of just saying you haven't been successful with "many post", you should show what you actually tried that didn't work. There isn't any way people reading this could know which methods you tried and which you haven't. – Claies Feb 23 '17 at 18:42
  • 1
    for example, you could use `var decoded = decodeURI(hu);`. – Claies Feb 23 '17 at 18:45
  • I have read many snippets and they work on their own like: var replaced = str.replace(/%20/g, " "); But I don't know how or where to place them. I am now trying to place the: var decoded = decodeURI(hu); in the script but I dont know where so I keep breaking thats script :( sorry – user2765741 Feb 23 '17 at 18:49
  • if you use that line I suggested, it would go right after `hu` and before `gy`, and your `gy` line would change to `gy = decoded.split("&");` – Claies Feb 23 '17 at 18:53
  • Thank you so much!! – user2765741 Feb 23 '17 at 19:03

1 Answers1

0

Use the replaced value as value of the form input like this:

    window.onload=function(){ 
        function querySt(ji) {
            var hu = window.location.search.substring(1); 
            var gy = hu.split("&");
            for (i=0;i<gy.length;i++) { 
                var ft = gy[i].split("=");  
                if (ft[0] == ji) { 
                    return ft[1];  
                } 
            }
        } 
        var id = querySt("id");
        if( id==null){ 
        }else{
            var replaced = id.replace(/%20/g, " ");
            document.getElementById('form_subject').value = replaced; 
        } 
    } 
Glia
  • 371
  • 2
  • 9