Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in Code, missing ';' 1

Status
Not open for further replies.

abienz

Programmer
Aug 13, 2001
53
GB
Can anyone tell me what's wrong with this part of code from some javascript.

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.", "if(thisURL!=null){
var urlUBBCode = "";
var urlUBBCode = &quot;<a href=&quot;+thisURL+&quot; class='b' target='_blank' onFocus='if(this.blur)this.blur()'>&quot;+thisURL+&quot;</a>&quot;;
revisedMessage = eval('document.form1.'+myTarget+'.value'+urlUBBCode);
eval('document.form1.'+myTarget+'.value+=\&quot;'+revisedMessage+'\&quot;;');
eval('document.form1.'+myTarget+'.focus();');
return;
}
}

The story is, I have a some formatting buttons that go above a couple of textareas, each textarea has it's own name and I pass this into the function in the variable myTarget, whenever I run this script I get an error for the line that goes var urlUBBCode = &quot;<a href=&quot;+..... saying that it's missing a ;

Is there a work around I should use?

Thanks in advance
 
abienz,
The error is that you try to define the same variable twise:
var urlUBBCode = &quot;&quot;;
var urlUBBCode = .....

Remove the 2nd &quot;var&quot; and it will be ok.

 
I'm not sure that that was entirely it, I've changed the following...


function DoPrompt(action,myTarget) {
var revisedMessage;
var currentMessage = eval('document.form1.'+myTarget+'.value');

if (action == &quot;url&quot;) {
var thisURL = prompt(&quot;Enter the complete URL for the link you wish to add.&quot;, &quot;if(thisURL!=null){
var urlUBBCode = &quot;&quot;;
var urlUBBCode = &quot;<a href=&quot;+thisURL+&quot; class='b' target='_blank' onFocus='if(this.blur)this.blur()'>&quot;+thisURL+&quot;</a>&quot;;
revisedMessage = eval('document.form1.'+myTarget+'.value')+urlUBBCode;
eval('document.form1.'+myTarget+'.value+=\&quot;'+revisedMessage+'\&quot;;');
eval('document.form1.'+myTarget+'.focus();');
return;
}
}

There is a slight change here believe me, and myTarget was origionally set as textbox instead of 'textbox' with the 's so now I get an error on the line that goes...
eval('document.form1.'+myTarget+'.value+=\&quot;'+revisedMessage+'\&quot;;');

the error is something to do with an unterminated string or something
 
First, I still see the same error:

var urlUBBCode = &quot;&quot;;
var urlUBBCode =

Second, statements like [tt]eval(something)[/tt] cannot be just &quot;flying in the air&quot;, you should assign it to some variable:

result = eval('document.form1.'+myTarget+'.focus();');
 
Ok I think I'm slowly making progress here,

I've taken out the reassignment of var urlUBBCode as suggested.

Now I'm in a situation where I'm not really sure as to how to use the eval function properly, perhaps you could guide me here. This is what I have so far.

function DoPrompt(action,myTarget) {
var revisedMessage;
var currentMessage = eval('document.form1.'+myTarget+'.value');

if (action == &quot;url&quot;) {
var thisURL = prompt(&quot;Enter the complete URL for the link you wish to add.&quot;, &quot; if(thisURL!=null){
var urlUBBCode = &quot;<a href='&quot;+thisURL+&quot;' class='b' target='_blank' onFocus='if(this.blur)this.blur()'>&quot;+thisURL+&quot;</a>&quot;;
revisedMessage = eval('document.form1.'+myTarget+'.value')+urlUBBCode;
var result = eval('document.form1.'+myTarget+'.value+=\&quot;'+revisedMessage+'\&quot;;');
eval('document.form1.'+myTarget+'.focus();');
return result;
}
}

I'm getting this error however...

Unterminated String constant, for the following line...

revisedMessage = eval('document.form1.'+myTarget+'.value')+urlUBBCode;

What am I doing wrong? any ideas?

TIA!
 
Write out in pure HTML what you're trying to do with the Javascript code first, then work from that to develop what you're trying to eval(). It looks like you may be missing an equal sign and quotation marks in revisedMessage. It looks like it'd be simpler just to send document.form1.myTarget directly to the function as an argument to work on the &quot;.value&quot; there, and it'd probably be faster.

As many times as you're using
eval('document.form1.'+myTarget+'.value');
in the function, just make the HTML form element the parameter passed, and then you can refer to it as myTarget.value directly.

 
I managed to find the solution and it's extremely simple, instead of doing this...

var currentMessage = eval('document.form1.'+myTarget+'.value');

I do this...

var currentMessage = document.form1[myTarget].value;

and so the problematic line becomes...

document.form1[myTarget].value=revisedMessage;

There you go! I couldn't believe it when Ifound out how easy i was.

thanks for the help guys
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top