By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,139 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,139 IT Pros & Developers. It's quick & easy.

Passing object reference via document.write string

P: n/a
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);

#################################################
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <c9*******************@news.demon.co.uk>,
ma**@notmeyeardley.demon.co.uk enlightened us with...

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.


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

Jul 23 '05 #2

P: n/a
Mark Anderson wrote:
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.


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/
Jul 23 '05 #3

P: n/a
Matt,

"Matt Kruse" <ne********@mattkruse.com> wrote in message
news:c9********@news3.newsguy.com...
Mark Anderson wrote:
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.
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/


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
Jul 23 '05 #4

P: n/a
Kaeli,

"kaeli" <ti******@NOSPAM.comcast.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <c9*******************@news.demon.co.uk>,
ma**@notmeyeardley.demon.co.uk enlightened us with...

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.

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


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
Jul 23 '05 #5

P: n/a
In article <c9*******************@news.demon.co.uk>,
ma**@notmeyeardley.demon.co.uk enlightened us with...


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.


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

Jul 23 '05 #6

P: n/a
Kaeli,

"kaeli" <ti******@NOSPAM.comcast.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <c9*******************@news.demon.co.uk>,
ma**@notmeyeardley.demon.co.uk enlightened us with...


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.


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


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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.