473,386 Members | 1,823 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,386 software developers and data experts.

can't solve this

I have the following Javascript piece of code in a .js file (it's a function
that generates a new html page with a button to call a function).

var str = "hello";
// some code
with (document) {
// some writes
writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
}
// some writes

and running it with InternetExplorer6 gives me the error:
--------------------------------
Line: 0
Error: 'str' is not defined
--------------------------------
and Mozilla console says the same.

Seems like it searches the string in a new blank javascript file, but i need
it uses the same file, where my code is.

Any help?
Jul 20 '05 #1
3 1395
"yukatan" <a@a.com> writes:
I have the following Javascript piece of code in a .js file (it's a function
that generates a new html page with a button to call a function).

var str = "hello";
This variable is declared inside a function. That means that it can only
be seen inside that function's body. The Javascript that you write will
not be inside this body, so "str" will not be defined for it.

Solution: move 'var str="hello";' outside the function. That will
make it a global variable.
// some code
with (document) {
// some writes
writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");


Don't use "javascript:".
<URL:http://jibbering.com/faq/#FAQ4_24>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
i understand the problem but i cant achieve what i want.
all the code i wrote before are inside a for loop like this:

for (int i=0;i<n;++i){
with (document) {
writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
}
}

so i want to create a table with n rows, but each one calling myOwnFunction
with the value of "i" as the parameter, that is, row 1 link will call
myOwnFunction('1'), row 2 link will call myOwnFunction("2"), etc.

and that is what i cant get it working.
plz help

"Lasse Reichstein Nielsen" <lr*@hotpop.com> escribió en el mensaje
news:pt**********@hotpop.com...
"yukatan" <a@a.com> writes:
I have the following Javascript piece of code in a .js file (it's a function that generates a new html page with a button to call a function).

var str = "hello";
This variable is declared inside a function. That means that it can only
be seen inside that function's body. The Javascript that you write will
not be inside this body, so "str" will not be defined for it.

Solution: move 'var str="hello";' outside the function. That will
make it a global variable.
// some code
with (document) {
// some writes
writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");


Don't use "javascript:".
<URL:http://jibbering.com/faq/#FAQ4_24>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'



Jul 20 '05 #3
Lee
yukatan said:

i understand the problem but i cant achieve what i want.
all the code i wrote before are inside a for loop like this:

for (int i=0;i<n;++i){
with (document) {
writeln("<TD><A HREF='javascript:myOwnFunction(str);'><IMG
SRC='images/picture.jpg' BORDER='0'></A></TD></TR>");
}
}

so i want to create a table with n rows, but each one calling myOwnFunction
with the value of "i" as the parameter, that is, row 1 link will call
myOwnFunction('1'), row 2 link will call myOwnFunction("2"), etc.

and that is what i cant get it working.


When you're posting to a technical newsgroup, particularly when
posting code, it would seem to make sense to take special care
to follow standard capitalization conventions to make your post
easier to read.

You're writing an entirely new page, which will not contain any
variables from the current page. You can either redefine str in
the new page, or write its literal value to the new page.
Also, don't declare variables as "int" and don't use "javascript:".

html="";
for (var i=0;i<n;++i){
html+="<TD><A HREF='#' onclick=myOwnFunction('"
+str
+"');'><IMG"
+"SRC='images/picture.jpg' BORDER='0'></A></TD></TR>";
}
document.write(html);

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Arne | last post by:
Hi, Probably an easy answer to this, but I have not been able to figure it out. How can I find the IP-address of the machine that my Java-program is running on ? Could not find any methods in...
17
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root()...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
13
by: Timothy Madden | last post by:
Hello all I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
5
by: Stephane | last post by:
Hi, I'm sorry, this question is also posted in dotnet.framework, I don't know in which newsgroup my question belongs to... I'm trying to write in the event log and it looks like it's any of my...
12
by: join | last post by:
I went to solve a problem related to Palindrome int isPalindrome(char *c); returns back 1 if the string is a palindrome and returns back zero if it is not a palindrome. but without using...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
3
by: Ryan Liu | last post by:
Hi, I have a big urgent problem to solve. I used to use Windows 2000 Chinese version, now I installed Windows XP (English) system. The problem is not about 2000 or XP, it is about English...
0
by: Guilherme Polo | last post by:
On Sat, Sep 20, 2008 at 4:10 PM, dmitrey <dmitrey15@ukr.netwrote: It is not only the button that doesn't respond, the entire application won't respond if you are blocking tcl from processing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.