Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > can't solve this

Reply
Thread Tools

can't solve this

 
 
yukatan
Guest
Posts: n/a
 
      11-06-2003
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?


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-06-2003
"yukatan" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
yukatan
Guest
Posts: n/a
 
      11-06-2003
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" <(E-Mail Removed)> escribió en el mensaje
news(E-Mail Removed)...
> "yukatan" <(E-Mail Removed)> 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 - (E-Mail Removed)
> DHTML Death Colors:

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





 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      11-06-2003
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);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If you can solve this... James Wireless Networking 3 10-23-2005 01:00 AM
how can I solve Common Language runtime Error Ajith Nair ASP .Net 0 08-17-2005 08:17 AM
How to solve COMException Ajith Nair ASP .Net 2 07-22-2005 01:55 AM
An Irritating Thunderbird Newsgroup Problem...Please Solve It! Ann Speakman Firefox 2 03-09-2005 12:08 AM
how to solve file.copy problem tony ASP .Net 3 08-26-2004 06:28 AM



Advertisments