Javascript Question | Community
Skip to main content
February 1, 2013
Solved

Javascript Question

  • February 1, 2013
  • 8 replies
  • 2549 views
The Marketo Article here: https://community.marketo.com/MarketoArticle?id=kA050000000L3Zb provides sample Javascript that you can use on Landing Pages to take an Email token value from a parameter and populate a form field.

Only problem is, if the parameter value isn't present, the Javascript makes the form field default to show the words "undefined".

Is there a small tweak we can make to the JS in that artice, to have the form field prepopulate with no value if the parameter value is missing?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

Hi Carrie,

Replace your landing page content block that contains the JavaScript code you added to prepopulate the email field on the landing page with the following code:

<script language="Javascript" src="/js/public/jquery-latest.min.js" type="text/javascript"></script>
<script src="/js/public/jQueryString-2.0.2-Min.js" type="text/javascript" ></script>
<script>
     // to set cookies.  Uses noConflict just in case
     var $jQ = jQuery.noConflict();
     var pEmail = $jQ.getQueryString({ ID: "Email" });
//--> I added this conditional statement <-- //
     if (! pEmail) {
         //alert('Email address is undefined!');
         document.getElementById("Email").setAttribute("value", '');
     };
</script>

Let me know how that goes!

Reade

8 replies

February 1, 2013
Hi Carrie,

You can try to add some conditional logic using the following examples:

if(typeof variable_here === 'undefined') {
   // your code here.
 };

or 

if(! variable_here) {
   // your code here.
};
If you can share your code here, I may be able to you a bit further.

Hope that helps.
February 1, 2013

Thanks for your response Reade, the code we're using is the same as that in the article:

<script language="Javascript" src="/js/public/jquery-latest.min.js" type="text/javascript"></script>
<script src="/js/public/jQueryString-2.0.2-Min.js" type="text/javascript" ></script>
<script>
  // to set cookies.  Uses noConflict just in case
  var $jQ = jQuery.noConflict();
  var pEmail = $jQ.getQueryString({ ID: "Email" });
document.getElementById("Email").setAttribute("value", pEmail);
</script>

February 1, 2013
Did you try the sample snippets and get it working? If not, can you also share the landing page you are using?
February 11, 2013
Hi Reade,

Thanks for providing those two suggestions, I tried both of these, however the "undefined" value was still appearing.

Having said that, I'm not particularly familiar with JS so I'm kind of shooting in the dark a little with where to insert it.

Any ideas?

Thanks,
Carrie
 
February 11, 2013
Hi Carrie,

Can you share the landing page you are using?

Reade
February 11, 2013
Hi Reade,

I can't share the actual page, however I've created a replica that functions the same in our own instance for you to take a look at here: insights.datarati.com.au/CarrieCommunityTest.html

If you're not cookied, you'll see the field prepopulates with "undefined" - we'd like it to be blank if the Email token isn't present in the URL parameter.

Thanks,
Carrie
Accepted solution
February 11, 2013

Hi Carrie,

Replace your landing page content block that contains the JavaScript code you added to prepopulate the email field on the landing page with the following code:

<script language="Javascript" src="/js/public/jquery-latest.min.js" type="text/javascript"></script>
<script src="/js/public/jQueryString-2.0.2-Min.js" type="text/javascript" ></script>
<script>
     // to set cookies.  Uses noConflict just in case
     var $jQ = jQuery.noConflict();
     var pEmail = $jQ.getQueryString({ ID: "Email" });
//--> I added this conditional statement <-- //
     if (! pEmail) {
         //alert('Email address is undefined!');
         document.getElementById("Email").setAttribute("value", '');
     };
</script>

Let me know how that goes!

Reade

February 11, 2013
Reade, you are a superstar!

This worked like a charm :)

Thank you!