473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing object reference via document.write string

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
6 1859
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Holger Butschek | last post by:
Hi folks, first of all, I'm absolutly new to javascript. I have designed a html-form and am trying to path parameters with the suffix of i.e. ?Seminartitel=hallowelt in the url. In the head...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
1
by: AA | last post by:
I have this simple class public class Person { public Person(){} public String Name = String.Empty; public String LastName = String.Empty; } and this procedure that has one input parameter...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
6
by: vncntj | last post by:
I have this home.js file and I'm trying to collect the values on the other at "emplist.aspx" function poponload() { testwindow= window.open ("emplist.aspx", "mywindow",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.