0

I have several forms synced to Salesforce currently running on a WordPress website using the Avada theme, but the client wants a new field added which will require adding an onload event in the body tag. I thought the easiest way to do this would be to go through the dashboard>Theme Options>Advanced>Code Fields but I don't see an option to add an event to the body tag. What's the best way to do this without editing the core template files (the forms are only on a few pages)?

Snippet of current code:

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST"><input type=hidden name="oid" value="00DU0000000H99p">
    <input type=hidden name="retURL" value="https://www.sablesys.com/thank-you/">
    <input type="hidden" name="member_status" value="Sent"/>
    <!-- Campaign id for Quote Campaign -->
    <input type="hidden" name="Campaign_ID" value="7010B000001Eiz3QAC" />
    <input type="hidden" id="lead_source" name="lead_source" value="Website">  
                <label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" required/><br>
        <label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" required/><br>
        <label for="company">Company</label><input id="company" maxlength="40" name="company" size="20" type="text" required/><br>
        <label for="email">Email</label><input id="email" maxlength="80" name="email" size="20" type="text" required/><br>
        <label for="city">City</label><input id="city" maxlength="40" name="city" size="20" type="text" required/><br>

        Notes:<br><textarea id="00NU0000003mN8A" name="00NU0000003mN8A" rows="25" type="text" wrap="soft" style="width: 100%"></textarea><br>
        <input type="submit" name="submit">
</form>

New code the client requested be added:

A new field for the form:


<input type="hidden" id="URL" name="URL" value="" >

A new onload event:

<body onload="document.getElementById('URL').value = document.location.href">

Is there a way to do a window.onload inside a script instead? I admit I don't know javascript well. Please be descriptive in your replies. Thanks.

Ash
  • 1
  • 1

1 Answers1

0

This line of code IS javascript.

document.getElementById('URL').value = document.location.href;

You just need a way to get it executed.

If any other javascript code is executing on your page, make it part of that. You could add an "onsubmit" event to the form, like so:

<form onsubmit="FillInURL()">
<input id="URL" type=hidden>
</form>

function FillInURL() {document.getElementById('URL').value = document.location.href;}
T G
  • 445
  • 3
  • 7
  • yes, I know that line of code is javascript. I just don't where to put it. I like your solution, creating an onsubmit event in the form itself. If I don't have another script on the page where do I put that last line of code? `function FillInURL() {document.getElementById('URL').value = document.location.href;}` – Ash Mar 30 '19 at 16:02
  • You can add anywhere you like. Probably before the form itself is best - perhaps before any of the HTML. It's just a question of how you want to organize your code. – T G Mar 31 '19 at 05:14