Namespace problem

S

SteveYoungTbird

I have a working javascript script with some global variables which I
want to use alongside other scripts. The script includes the following:

var side_bar_html = "";
var gmarkers = [];
var icount = 0;
function createMarker(point,name,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "mouseover", function() {
marker.openInfoWindow(html);
});
GEvent.addListener(marker, "click", function() {
drawMap(point);
});
READ.gmarkers[READ.icount] = marker;
READ.side_bar_html += '<a href="javascript:myclick(' + READ.icount
+ ')">' + name + '</a><br>';
READ.icount++;
return marker;
};
function myclick(icount) {
GEvent.trigger(gmarkers[icount], "mouseover");
};

I therefore have put the global variables into a namespace like this:

var READ = {};
READ.side_bar_html = "";
READ.gmarkers = [];
READ.icount = 0;
function createMarker(point,name,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "mouseover", function() {
marker.openInfoWindow(html);
});
GEvent.addListener(marker, "click", function() {
drawMap(point);
});
READ.gmarkers[READ.icount] = marker;
READ.side_bar_html += '<a href="javascript:myclick(' + READ.icount
+ ')">' + name + '</a><br>';
READ.icount++;
return marker;
};
function myclick(READ.icount) {
GEvent.trigger(READ.gmarkers[READ.icount], "mouseover");
};

But if I try to use the script with the namespace I get a Firebug error
"missing ) after formal parameters. function myclick(READ.icount) {\n"

It's getting late and I can't seem to work this out. Can anyone help?
 
S

SteveYoungTbird

Sorry I have a bit of a "cut & paste" problem here. The script with
global variables should read:

var side_bar_html = "";
var gmarkers = [];
var icount = 0;
function createMarker(point,name,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "mouseover", function() {
marker.openInfoWindow(html);
});
GEvent.addListener(marker, "click", function() {
drawMap(point);
});
gmarkers[icount] = marker;
side_bar_html += '<a href="javascript:myclick(' + icount +
')">' + name + '</a><br>';
icount++;
return marker;
};
function myclick(icount) {
GEvent.trigger(gmarkers[icount], "mouseover");
};

Steve.
 
R

RobG

SteveYoungTbird wrote:
Sorry I have a bit of a "cut & paste" problem here. The script with
global variables should read:

Please don't top-post here. Just correct the original and re-post.


[...]
I have a working javascript script with some global variables which I
want to use alongside other scripts. The script includes the following:
var side_bar_html = "";
var gmarkers = [];
var icount = 0;
function createMarker(point,name,html) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindow(html);
    });
        GEvent.addListener(marker, "click", function() {
        drawMap(point);
    });
    READ.gmarkers[READ.icount] = marker;
    READ.side_bar_html += '<a href="javascript:myclick(' + READ..icount +
')">' + name + '</a><br>';
    READ.icount++;
    return marker;
};
function myclick(icount) {
    GEvent.trigger(gmarkers[icount], "mouseover");
};
I therefore have put the global variables into a namespace like this:
var READ = {};
READ.side_bar_html = "";
READ.gmarkers = [];
READ.icount = 0;
function createMarker(point,name,html) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindow(html);
    });
        GEvent.addListener(marker, "click", function() {
        drawMap(point);
    });
    READ.gmarkers[READ.icount] = marker;
    READ.side_bar_html += '<a href="javascript:myclick(' + READ..icount +
')">' + name + '</a><br>';
    READ.icount++;
    return marker;
};
function myclick(READ.icount) {

In a function declaration, the formal parameter list (the bit between
the brackets) is essentially the same as a local variable declaration
and must follow the same rules for identifiers (they can't have period
characters in them). I think you went a bit overboard in applying your
"namespace".

You probably want:

function myclick(icount) {

    GEvent.trigger(READ.gmarkers[READ.icount], "mouseover");

and

GEvent.trigger(READ.gmarkers[icount], "mouseover");


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top