Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing object reference via document.write string

Mark Anderson
Guest
 
Posts: n/a
#1: Jul 23 '05
I have code to develop result page links (like a search engine) for some
results being passed from a database where I've no server-sdide acces -
thus JS.

The code is below and works fine except the function in the onClick
event of the link being written on screen fails indicating 'theForm' as
being passed through is 'not defined'. The form is being passed as
"document.resultAdd" and can be checked as arriving in my function.

So what am I doing wrong?

FWIW, if I place the alert in the calling code (see code example) I see
passed object in the string as [object] - is that what I should expect?

Regards

Mark

################################################## ##
//curOffset - offset number in total items (parsed from GET method URL)
//numPerPage - (number) number of records per result page
//totalItems - (number) total items in records set (at n per page)
//numLinks - (number) number of links to create (odd number)
//linkName,thePage,task,theForm - vars passed through for string being
written

function
makeNavLinks(curOffset,numPerPage,totalItems,numLi nks,linkName,thePage,t
ask,theForm){
var numPages= 1 + Math.floor(totalItems/numPerPage);
var strLinks = '',theOffset = '',newOffset = '',newPage = '',start =
0, i = 0;
var linkStubA = 'Jump to page: -  ';
var linkStubB = '<a href="#" onClick="alert(theForm);goWhere(\'' +
linkName + '\',\'';
var linkStubC = '\',\'' + task + '\',' + theForm + '); return
false;">';
var linkStubD = '</a>&nbsp;&nbsp;';
if (numPages == 1){
strLinks = linkStubA + numPages;
return strLinks;
} else {
var curPage = ((curOffset/numPerPage) + 1);
var halfLink = Math.floor(numLinks/2);
if (numLinks > numPages) {
numLinks = numPages
}
if ((curPage-halfLink) < 1) {
i = 1;
} else if ((curPage + halfLink) > numPages) {
i= (numPages-numLinks);
} else {
i = (curPage - halfLink);
}
strLinks = linkStubA;
for ( var k = i ; k < (i + numLinks); k ++){
if (((k-1)* numPerPage)== curOffset) {
strLinks += k + '&nbsp;&nbsp;';
} else {
thisOffset = ((k - 1) * numPerPage).toString();
newOffset = '&offset=' + thisOffset;
newPage = thePage.replace(/&offset=\d+/,newOffset);
strLinks += linkStubB + newPage + linkStubC + k + linkStubD
+'\n\n';
}
}
return strLinks;
}
}

////////////////////////
...called by... (%totalItems% is a server-side macro)
var x =
makeNavLinks(pageOffset,myResultsPerPage,%totalite ms%,9,'PAGELIST',thisP
age,'ADD',document.resultAdd);
//alert(x)
document.write(x);

#################################################



kaeli
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Passing object reference via document.write string


In article <c9ih60$19j$1$8300dec7@news.demon.co.uk>,
mark@notmeyeardley.demon.co.uk enlightened us with...[color=blue]
>
> The code is below and works fine except the function in the onClick
> event of the link being written on screen fails indicating 'theForm' as
> being passed through is 'not defined'. The form is being passed as
> "document.resultAdd" and can be checked as arriving in my function.
>[/color]

A form, declared as
<form name="resultAdd">

should be
document.forms["formAdd"]
to be passed properly in a cross-browser manner.

As to this
document.write(x);

Note that writing to the current document tends to wipe out existing
content. Since I have no context, I can't say for sure what it does in
your application, but try commenting it out. If commenting it out fixes
your problem, you'll need to find another way to acomplish whatever it
what accomplishing. If you need it for NN4, write to a layer instead. If
using NN6/IE6, use divs and write to that.

--
--
~kaeli~
Why did kamikaze pilots wear helmets?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Matt Kruse
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Passing object reference via document.write string


Mark Anderson wrote:[color=blue]
> The code is below and works fine except the function in the onClick
> event of the link being written on screen fails indicating 'theForm'
> as being passed through is 'not defined'. The form is being passed as
> "document.resultAdd" and can be checked as arriving in my function.[/color]

Once the code is written out, it becomes a string and all references are
lost.

Consider passing around the form name instead, so you can always get access
to the form object by doing document.forms[formName]. Since the name is a
simple string, it can be passed around and written out without losing
references.

Unsolicited Observation: Why are you doing this in js rather than
server-side? It would seem like the entire content would be better built on
the server-side, rather than client-side.

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/


Mark Anderson
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Passing object reference via document.write string


Matt,

"Matt Kruse" <newsgroups@mattkruse.com> wrote in message
news:c9ii9m0ush@news3.newsguy.com...[color=blue]
> Mark Anderson wrote:[color=green]
> > The code is below and works fine except the function in the onClick
> > event of the link being written on screen fails indicating 'theForm'
> > as being passed through is 'not defined'. The form is being passed[/color][/color]
as[color=blue][color=green]
> > "document.resultAdd" and can be checked as arriving in my function.[/color]
>
> Once the code is written out, it becomes a string and all references[/color]
are[color=blue]
> lost.
>
> Consider passing around the form name instead, so you can always get[/color]
access[color=blue]
> to the form object by doing document.forms[formName]. Since the name[/color]
is a[color=blue]
> simple string, it can be passed around and written out without losing
> references.
>
> Unsolicited Observation: Why are you doing this in js rather than
> server-side? It would seem like the entire content would be better[/color]
built on[color=blue]
> the server-side, rather than client-side.
>
> --
> Matt Kruse
> Javascript Toolbox: http://www.mattkruse.com/javascript/[/color]

Thanks, I'll give that a go.

As to your question, you'll notice I say that I don't have any
server-side access - period either to the server or to the 3rd party DLL
passing out the records. Don't shoot the messenger! I do realise that
if circumstances allow I should do this server-side but it's not my
source app, server, etc....

Regards

Mark


Mark Anderson
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Passing object reference via document.write string


Kaeli,

"kaeli" <tiny_one@NOSPAM.comcast.net> wrote in message
news:MPG.1b2688a49f68f752989eb4@nntp.lucent.com...[color=blue]
> In article <c9ih60$19j$1$8300dec7@news.demon.co.uk>,
> mark@notmeyeardley.demon.co.uk enlightened us with...[color=green]
> >
> > The code is below and works fine except the function in the onClick
> > event of the link being written on screen fails indicating 'theForm'[/color][/color]
as[color=blue][color=green]
> > being passed through is 'not defined'. The form is being passed as
> > "document.resultAdd" and can be checked as arriving in my function.
> >[/color]
>
> A form, declared as
> <form name="resultAdd">
>
> should be
> document.forms["formAdd"]
> to be passed properly in a cross-browser manner.
>
> As to this
> document.write(x);
>
> Note that writing to the current document tends to wipe out existing
> content. Since I have no context, I can't say for sure what it does in
> your application, but try commenting it out. If commenting it out[/color]
fixes[color=blue]
> your problem, you'll need to find another way to acomplish whatever it
> what accomplishing. If you need it for NN4, write to a layer instead.[/color]
If[color=blue]
> using NN6/IE6, use divs and write to that.
>
> --
> --
> ~kaeli~
> Why did kamikaze pilots wear helmets?
> http://www.ipwebdesign.net/wildAtHeart
> http://www.ipwebdesign.net/kaelisSpace
>[/color]

No better, I'm afraid. Same result.

Noo - we need to document.write(x) - it the outcome of the process! 'x'
is the string that writes out a set of 9 page HTML links each with an
onClick event which checks for unsubmitted current page items before
going to the next page. The script runs inline during page load so
nothings overwritten - code is old fashioned top-down code, i.e. not
using x/y positioned DIVs etc.

Regards

Mark


kaeli
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Passing object reference via document.write string


In article <c9imhj$h05$1$8302bc10@news.demon.co.uk>,
mark@notmeyeardley.demon.co.uk enlightened us with...[color=blue][color=green]
> >[/color]
>
> No better, I'm afraid. Same result.
>
> Noo - we need to document.write(x) - it the outcome of the process! 'x'
> is the string that writes out a set of 9 page HTML links each with an
> onClick event which checks for unsubmitted current page items before
> going to the next page. The script runs inline during page load so
> nothings overwritten - code is old fashioned top-down code, i.e. not
> using x/y positioned DIVs etc.
>[/color]

Ah, that makes more sense now.

I think Matt nailed it for you. Did you get this fixed?

--
--
~kaeli~
Well, aren't we just a flipping ray of sunshine?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Mark Anderson
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Passing object reference via document.write string


Kaeli,

"kaeli" <tiny_one@NOSPAM.comcast.net> wrote in message
news:MPG.1b28f75493c3a004989eb7@nntp.lucent.com...[color=blue]
> In article <c9imhj$h05$1$8302bc10@news.demon.co.uk>,
> mark@notmeyeardley.demon.co.uk enlightened us with...[color=green][color=darkred]
> > >[/color]
> >
> > No better, I'm afraid. Same result.
> >
> > Noo - we need to document.write(x) - it the outcome of the process![/color][/color]
'x'[color=blue][color=green]
> > is the string that writes out a set of 9 page HTML links each with[/color][/color]
an[color=blue][color=green]
> > onClick event which checks for unsubmitted current page items before
> > going to the next page. The script runs inline during page load so
> > nothings overwritten - code is old fashioned top-down code, i.e. not
> > using x/y positioned DIVs etc.
> >[/color]
>
> Ah, that makes more sense now.
>
> I think Matt nailed it for you. Did you get this fixed?
>
> --
> --
> ~kaeli~
> Well, aren't we just a flipping ray of sunshine?
> http://www.ipwebdesign.net/wildAtHeart
> http://www.ipwebdesign.net/kaelisSpace
>[/color]

Sorry, I figured this had run out of steam. I did solve the issue by
splitting the function in two - the bit with the object reference is now
in a JS library and the object required (one of a small number of form
names) is resolved there based on strings passed from the first,
dynamically written function. Not ideal as the form names are now hard
coded in the library function but this is one-off for someone else
who'll never touch the code. So, not ideal in many senses, but it works
and if fit for purpose in the context of use.

I was going to say all the suggestions failed but I realise I'd
misunderstood Matt's idea. I tried passing document.forms[formName] when
he mean passing formName and using it later as document.forms[formName].
Oh well, now I have 2 solutions <g>.

Thanks to all,

Mark


Closed Thread


Similar JavaScript / Ajax / DHTML bytes