mandava
mandava

Reputation: 346

string formatting an anchor which has to call a javascript function onclick(mvc)

   goog_snippet_vars = function(){
var w = window;
w.google_conversion_id ="xxxxx";
w.google_conversion_label ="xxxxxx";
w.google_conversion_value ="xxxxxx";
},
goog_report_conversion = function(url) {
goog_snippet_vars();
window.google_conversion_format = '3';
window.google_is_call = true;
var opt = new Object();
opt.onload_callback = function() {
if (typeof(url) != 'undefined') {window.location = url;}
}
var conv_handler = window['google_trackConversion'];
if (typeof(conv_handler) == 'function') {conv_handler(opt);}
}
</script>

I am dumping the above code in to a page(onload) using stringbuilder from server. and i am using string.format to generate html from server side to the telephone no displayed on the webpage , which on click have to call one of the functions in above script. I used the below code for that

string.Format("<a href=\"tel:{0}\" onclick=\"Javascript: goog_report_conversion(tel{0}); return false;\">{0}</a>", PhoneNumber));

everything is fine html is applying to the tele no. the issue is on click it is not hitting the function , its simply navigating to new page with url as href value.

so please help me if any work around is there.....thanks...

Upvotes: 4

Views: 1895

Answers (2)

mandava
mandava

Reputation: 346

Finally I fixed the issue. The main problem is passing the arguments to the script function. we have to pass some thing like goog_report_conversion(567678899);

For that I had used regex.replace function to remove special characters like below

Regex.Replace(sPhone,"[()' '-]","");

and passed that in the place of parameter at function call. now my generated html looks like below

<a onclick="Javascript: goog_report_conversion(2818669180); return false;" href="tel:(281) 866-9180">(281) 866-9180</a>

So, Finally I fixed my issue in the above manner. Thanks to all who tried to help me....

Upvotes: 3

Gr&#252;se
Gr&#252;se

Reputation: 1175

Please try with this as your first code snippet

<script type="text/javascript">
    goog_snippet_vars = function() {
        var w = window;
        w.google_conversion_id ="xxxxx";
        w.google_conversion_label ="xxxxxx";
        w.google_conversion_value ="xxxxxx";
    };
    goog_report_conversion = function(url) {
        goog_snippet_vars();
        window.google_conversion_format = '3';
        window.google_is_call = true;
        var opt = new Object();
        opt.onload_callback = function() {
            if (typeof(url) != 'undefined') {window.location = url;}
        }
        var conv_handler = window['google_trackConversion'];
        if (typeof(conv_handler) == 'function') {conv_handler(opt);}
    };
</script>

And with something like this for your second snippet:

string.Format("<a href=\"tel:{0}\" onclick=\"javascript:goog_report_conversion('tel:{0}'); return false;\">{0}</a>", PhoneNumber));

Then, could you post the resulting HTML that your program spits out? (Just the a tag including its attributes should suffice).

UPDATE: I think your telephone number format leads to errors. You wrote that the result of your string.Format call is

<a onclick="Javascript: goog_report_conversion(tel(xxx) xxx-xxxx); return false;" href="tel:(xxx) xxx-xxxx">(xxx) xxx-xxxx</a>

with x's representing numbers. There are two problems with this:

  1. This is a JS syntax error, the parameter to goog_report_conversion(url) should be a string, something like goog_report_conversion('tel:xxxxxxxxxx'). This error also prevents the return false; from being executed.

  2. The phone number format might be a problem, although I'm not sure about this, because I'm not sure what format tel: URLs can have. You should probably get rid of brackets/parentheses and whitespace in the number and adhere to the guides I am linking here: possibly helpful link 1, possibly helpful link 2

UPDATE 2: This link might be useful, it describes the syntax for "tel:" URLs.

Hope that helps! :)

Upvotes: 0

Related Questions