0

I have some working code (jQuery/Javascript) that makes a call to an API and submits data to it. The same service then returns a success or failure message depending on whether the data was inserted into the API db. The below works flawlessly when loaded in the browser.

function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }

        $(document).ready(function () {
            var groupType = getParameterByName('group').trim();

            if (groupType == 'm') {
                groupId = 'ICM.RealLife.Mobile';
            } else if (groupType == 'd') {
                groupId = 'ICM.RealLife.Desktop';
            }

            var email = getParameterByName('email').trim();
            var mobileTel = getParameterByName('mobile').trim();
            var panelistId = mobileTel;
            var password = 'icm001';
            var locale = 'en';

            alert('email=' + email + '\n\nMobile=' + mobileTel + '\n\nGroup=' + groupId);

            if (mobileTel != '' && email != '' && groupId != '') {
                //Build up querystring to pass to API
                var dataString = "panelistId=" + (encodeURIComponent('+') + mobileTel) + "&groupId=" + groupId + "&emailAddress=" + email + "&password=" + password + "&locale=" + locale + "&mobileNumber=" + (encodeURIComponent('+') + mobileTel) + "";
                //var apiResult;
                //send to API
                $.getJSON('https://www.analyzeme.net/api/server/prereg/?', dataString + '&callback=?', function (getResult) {
                    //apiResult = JSON.stringify(getResult);
                    //alert(apiResult);
                });
            //} else {
            //    alert('Incorrect parameters!');
            }
        });

I now have to get this working using a 1x1 tracking pixel using aspx like below;

<img src="http://www.somedomain.com/[email protected]&mobile=+441111222222&group=d" width="1" height="1"/>

BUT, I do not know how to get my JavaScript to fire in the asp.net page when it is hit? I know I need to do something with RegisterStartupScript but how do I get all that JS into it and how do I get it to fire when the page is hit. I know how to return an img/gif using response headers, so I am cool with that.

Help greatly appreciated! :)

1 Answer 1

1

Call the JS function from your Page_Load event in code behind. This will fire every time the page is loaded.

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, GetType(), "myFunction", "myFunction();", true);
}

JavaScript

function myFunction() {
    //Code you want to run from document.ready
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the delay. I totally forgot to mark this as resolved.

Your Answer

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.