Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Problem with using function variables


Message #1 by "Alexander Bienz" <alex@v...> on Thu, 7 Feb 2002 16:12:06
Ok, now I've got another problem here,  sorry about this! :(

this is another function that is similar

function DoPrompt(action,myTarget) {
	var revisedMessage;
	var currentMessage = eval('document.form1.'+myTarget+'.value;');
	if (action == "url") {
	var thisURL = prompt("Enter the complete URL for the link you wish to
add.", "http://");
if(thisURL!=null){
	var urlUBBCode = "";
var urlUBBCode = "<a href="+thisURL+"
onFocus='if(this.blur)this.blur()'>"+thisURL+"</a>";
	revisedMessage = currentMessage+urlUBBCode;
	eval('document.form1.'+myTarget+'.value+=\"'+revisedMessage+'\";');
	eval('document.form1.'+myTarget+'.focus();');
	return;
	}
}

This is only the top part of the function, after this come an else if with
almost the same piece of code, but that's not important right now...

Where I have var currentMessage 
eval('document.form1.'+myTarget+'.value;'); I am getting an error saying
"unterminated string constant"  any idea why this is?

		Cheers,
			Al.

> -----Original Message-----
> From: Zach Kenyon [mailto:zantispam@n...]
> Sent: 07 February, 2002 4:30 PM
> To: javascript
> Subject: [javascript] RE: Problem with using function variables
>
>
> "Alexander Bienz" <alex@v...> wrote:
>
> >As you can see it runs another function called AddText, with two
> >variables myTarget and AddTxt,
>
> Which are both passed by value by default.  Which is your problem.
>
> >function AddText(myTarget,NewCode) {
> >    if (myTarget == "CaseImage1") {
> >    document.form1.CaseImage1.value+=NewCode;
> >    }
> >}
>
> Should be:
> eval('document.form1.'+myTarget+'.value+='+NewCode+';');
>
> >Now when I run the page and click the link, nothing happens,
> and I'm not
> >sure as to why?
>
> That's because myTarget and NewCode, while defined in the
> function, are undefined outside of the function.  Read up on
> variable scope in javascript for more info.
>
> >I have also had the AddTest function written like this...
> >
> >function AddText(myTarget,NewCode) {
> >    document.form1.myTarget.value+=NewCode;
> >}
> >
> >This way didn't work either it came up with an error that
> myTarget was
> >null or not an object,
>
> Again, because myTarget was passed by value.  Inside the
> function, it's a string.  Outside the function, it's an
> object.  See the problem?
>
> eval() makes the interpreter evaluate the result of variable
> substitutions within the parentheses.
>
> Therefore,
> eval('document.form1.'+myTarget+'.value+=\"'+NewCode+'\";');
> returns
> ducument.form1.CaseText1.value+="<b>foo</b>";
> which (due to javascript not having strong types) evaluates
> as you might expect.
>
> HTH
> --
> Zach Kenyon
> Web Devloper / Analyst
>  xxx.xxx.xxxx
> zantispam@n...
>
>
>
> __________________________________________________________________
> Your favorite stores, helpful shopping tools and great gift
> ideas. Experience the convenience of buying online with
> Shop@N...! http://shopnow.netscape.com/
>
> Get your own FREE, personal Netscape Mail account today at
http://webmail.netscape.com/




  Return to Index