TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Getting Error: is not a function when viewed in firefox - works in IE (http://www.talkphp.com/javascript-ajax-e4x/3710-getting-error-not-function-when-viewed-firefox-works-ie.html)

almac007 12-04-2008 01:16 AM

Getting Error: is not a function when viewed in firefox - works in IE
 
Hi Guys,

Could someone please help me...

I am trying to fix up the site www.thinkpharmacy.com.au

On the right hand side of the home page there are some ads whcih are randomly generated. If you are browsing with Internet Explorer and you click one of these ads you get taken to that particular products page. everything works fine.

The problem i am having is that if i am browsing the site using Firefox and i click one of the ads all i get the following error message...

Error: gid("linkAds" + adsClicked).click is not a function
Source File: http://www.thinkpharmacy.com.au/js/ads.js
Line: 85


I cant understand why this is not working.

Here is a copy of the script ads.js that is being called.

javascript Code:
var xmlHttpAds
   
    function stateChangedAds()
        {
            if (xmlHttpAds.readyState==4)
            {
                //tamResp = xmlHttpAds.responseText.toString().length;
                //if(tamResp > 1){
                    openAds();
                    //alert(xmlHttpAds.responseText);
                    //xmlHttpAds = null;
                //}
            }
        }
       
    function GetxmlHttpObjectAds()
        {
        var xmlHttpAds=null;
        try
          {
          // Firefox, Opera 8.0+, Safari
          xmlHttpAds=new XMLHttpRequest();
          }
        catch (e)
          {
          // Internet Explorer
          try
            {
            xmlHttpAds=new ActiveXObject("Msxml2.XMLHTTP");
            }
          catch (e)
            {
            xmlHttpAds=new ActiveXObject("Microsoft.XMLHTTP");
            }
          }
        return xmlHttpAds;
    }
    var product = '';
    var adsClicked = '';
    var Urlproduct = '';
    function updateAds(id,product_id,Url){
        adsClicked = id;
        xmlHttpAds = GetxmlHttpObjectAds();
        var url = "";
        // DMD - 20060626 - get the host info
        // and make the URL
        switch(window.location.hostname)
        {
        case 'www.thinkpharmacy.com.au':
          var serverNameURL = "http://www.thinkpharmacy.com.au";
          break;   
        case 'tpo.thinkpharmacy.com.au':
          var serverNameURL = "https://tpo.thinkpharmacy.com.au";
          break;
        case 'www.productezine.com':
          var serverNameURL = "https://www.productezine.com/testserver/think";
          break;
        default:
          var serverNameURL = "";
        }
        //alert('passed in is ' + window.location.hostname);
        //alert('servernameurl is ' + serverNameURL);
        // var url="http://www.productezine.com/testserver/think/ajax/updateAds.php?id="+id;
        var url=serverNameURL+"/ajax/updateAds.php?id="+id; 
       
        var url2="http://www.thinkpharmacy.com.au/ajax/updateAds.php?id="+id;   
    //  alert('url2 is ' + url2);
       
        // DMD Edit
    //  alert(url);
        product = product_id;
        xmlHttpAds.onreadystatechange=stateChangedAds;
    //  alert('before redirect');
        xmlHttpAds.open("GET",url,true);
    //  alert('after redirect');
        xmlHttpAds.send(null);
        Urlproduct = Url;
    //  alert('URL Product is ' + Urlproduct);
    }

    function openAds(){
        if(Urlproduct == ""){
            gid("linkAdsProd"+adsClicked).click();
        }else{
            gid("linkAds"+adsClicked).click();
        }
    }


if anybody could help me out i would be very apprieciative.

Thanks in advance,

Almac007

Wildhoney 12-04-2008 01:58 AM

I don't see how that can work in any browser, if that's the exact code you're using. You're missing curly brackets, and also the function syntax for those classes are wrong. The syntax is as so:

javascript Code:
var TalkPHP =
{
    MyFunction1: function()
    {
        alert('My Function 1');
    },

    MyFunction2: function()
    {
        alert('My Function 2');
    }
}

TalkPHP.MyFunction1();
TalkPHP.MyFunction2();

Are you certain that's the precise code that you're using?

almac007 12-04-2008 02:03 AM

Hi Wildhoney,

Thanks for getting back to me.

Yeah that is the code cut and pasted from teh script. I was not the one who wrote it and am simply trying to fix it.

When you say that function brackets are missing are you referring to var xmlHttpAds?

and if so would it be something like

var xmlHttpAds =
{

...functions in here.
}

Thanks again for your help.

almac007

Wildhoney 12-04-2008 02:24 AM

Yep, that's the one! And also reverse the function and function names, as seen below. Hopefully this should get it working, but don't hesitate to re-paste the new code if it still doesn't work.

javascript Code:
/* Don't forget the equals symbol. */
var TalkPHP =
{
    /* This is the RIGHT way. */
    MyFunction1: function()
    {
        alert('My Function 1');
    },
    /* Don't forget a comma at the end of
    a function, before you start another. */


    /* This is the WRONG way. */
    /* Don't forget the colon after "function" */
    function: MyFunction2()
    {
        alert('My Function 2');
    }
    /* No need for a comma here unless you're
    adding another function below it. */

}

Remember: functionName: function() :-)

almac007 12-04-2008 04:15 AM

Hi Wildhoney,

Thanks for all your help with this.

I have rewritten the code but now i cant get it to work in either IE or Firefox. I keep getting the follwoing error.

Error: updateAds is not defined
Source File: javascript:updateAds('73','0','/prodinfo.php?id=1252')
Line: 1

Its probably something really simple :(

here is a copy of the rewritten code.

var xmlHttpAds =
{

var product = '';
var adsClicked = '';
var Urlproduct = '';

stateChangedAds: function()
{
if (xmlHttpAds.readyState==4)
{
openAds();
}
},
GetxmlHttpObjectAds: function()
{
var xmlHttpAds=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttpAds=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttpAds=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttpAds=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttpAds;
},
updateAds: function(id,product_id,Url)
{
adsClicked = id;
xmlHttpAds = GetxmlHttpObjectAds();
var url = "";
// DMD - 20060626 - get the host info
// and make the URL
switch(window.location.hostname)
{
case 'www.thinkpharmacy.com.au':
var serverNameURL = "https://www.thinkpharmacy.com.au";
break;
case 'tpo.thinkpharmacy.com.au':
var serverNameURL = "https://tpo.thinkpharmacy.com.au";
break;
case 'www.productezine.com':
var serverNameURL = "https://www.productezine.com/testserver/think";
break;
default:
var serverNameURL = "";
}

var url=serverNameURL+"/ajax/updateAds.php?id="+id;

product = product_id;
xmlHttpAds.onreadystatechange=stateChangedAds;
xmlHttpAds.open("GET",url,true);
xmlHttpAds.send(null);
Urlproduct = Url;
},
openAds: function()
{
if(Urlproduct == ""){
gid("linkAdsProd"+adsClicked).click();
}else{
gid("linkAds"+adsClicked).click();
}
}
}


thanks again for your help.

sketchMedia 12-04-2008 03:18 PM

I think it has more to do with the fact that you are using an object literal, yet are still trying to assign variables in a normal fashion.
javascript Code:
obj = {
    var test = '';
    var test2 = '';
}
is incorrect, this is the correct way and looks similar to the methods:
javascript Code:
obj = {
    test : '',
    test2 : '',
}
Basically (looking at the error from your site currently) JS is trying to interpret 'var' as a property of the object, then it will look for ':' to assign a value to it but it cant find it. Also you don't assign properties with values in that way, you assign them like functions (in theory you are with methods, assigning an anonymous function to a object property) also the comma at the end is important too.

Salathe 12-04-2008 04:48 PM

Are you guys seeing the same code as me (in the original post)? I don't see a badly-written object, but a number of separate functions (4) and variables (4) in the global scope.

sketchMedia 12-04-2008 05:00 PM

rofl, i must admit i only scan read it.
Sigh its been a long day.

anyway the real error must be something to do with 'gid', is 'click' a property or a method? try knocking the '()' off the end. where is 'gid' defined?

almac007 12-04-2008 11:19 PM

Hi Salathe,

So should i be using the code from the original post or use the code that i have rewritten in the above post?

The code from the original post works in IE but not in firefox, where as the rewritten code does not work in either browser. :(

Thanks for all your help guys. This forum is great!

Cheers,

Almac007

sketchMedia 12-05-2008 09:27 AM

After looking over your code, i noticed that 'gid' returns a ref to and element via getElementById(), as a result you will need to re write this:
javascript Code:
function openAds(){
    if(Urlproduct == ""){
        gid("linkAdsProd"+adsClicked).click();
    }else{
        gid("linkAds"+adsClicked).click();
    }
}
As the 'elementRef.click()' method doesn't work in FF.

Your old code should be fine.

EDIT:
Im assuming that the element returned by gid in this case is a link of some sort? if so you could do something like this
javascript Code:
function openAds(){
    if(Urlproduct == ""){
        window.location = gid("linkAdsProd"+adsClicked).href;
    }else{
        window.location = gid("linkAds"+adsClicked).href;
    }   
}
I havent tested, i just thought it up now.


All times are GMT. The time now is 04:04 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0