can't solve this

Y

yukatan

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?
 
L

Lasse Reichstein Nielsen

yukatan said:
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
 
Y

yukatan

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
 
L

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);
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top