0

I am just picking up on javascript and I need to load 2 functions that I want to use when the window loads.

One function captures the form name and UTM parameters while the other generates a unique number. All these on window.onload in javascript for an HTML page.

My script below.

<script type="text/javascript">
var defaultHiddenFieldNameValue = "";
function getQueryStringParamValue(strQStrParam) {
var strURL = document.location.href;
var strQStrParamValue = "";
 if (strURL.indexOf('?') != -1)
 {
 strQStrParamValue = strURL.substr(strURL.indexOf('?') + 1);
 if (strQStrParamValue.indexOf(strQStrParam) != -1)
 {
 strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf(strQStrParam));
 strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf('=') + 1);
 if (strQStrParamValue.indexOf('&') != -1)
 strQStrParamValue = strQStrParamValue.substr(0, strQStrParamValue.indexOf('&'));
 return strQStrParamValue;
 }else{
 strQStrParamValue = defaultHiddenFieldNameValue;
 return strQStrParamValue;
 }
 }else{
 strQStrParamValue = defaultHiddenFieldNameValue;
 return strQStrParamValue;
 }
}
// Form name goes here
var form = "formname";
function setCampaign(){
 var elqForm = document.forms[form];
 //repeat for each field to populate
 document.forms["formname"]["val1"].value = getQueryStringParamValue('utm_source');
 document.forms["formname"]["val2"].value = getQueryStringParamValue('utm_medium'); 
 document.forms["formname"]["val3"].value = getQueryStringParamValue('utm_campaign');
}
function VisitorID() {
 var min = 1;
 var max = 9999999999;
 var num = Math.floor(Math.random() * (max - min + 1)) + min;
 var timeNow = new Date().getTime();
 document.getElementById('field16').value = num + '_' + timeNow;
}
function both() {
 function VisitorID();
 function setCampaign();
}
window.onload = both;
</script>
Sotiris Kiritsis
3,3563 gold badges25 silver badges31 bronze badges
asked Jul 29, 2017 at 14:57
1
  • Remove the two function syntax errors and itll work fine.. Commented Jul 29, 2017 at 14:58

2 Answers 2

1
function both() {
 function VisitorID();//syntax error
 function setCampaign();
}

The function keyword just goes before function declarations. If you want to call them you dont need them (they throw an error):

function both() {
 VisitorID();
 setCampaign();
}
answered Jul 29, 2017 at 15:00
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jonas, works amazing !! Appreciate your quick response.
@Arun if the answer helped you, then click the green tick below the voting buttons
1

You can not call functions with prefix function in it. That will declare the function. Change last few lines from this -

function both() {
 function VisitorID();
 function setCampaign();
}
window.onload = both;

to this -

function both() {
 VisitorID();
 setCampaign();
}
window.onload = both();

if you want both functions to get executed.

answered Jul 29, 2017 at 15:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.