Forum Moderators: open

Message Too Old, No Replies

Javascript-ASP conditional statement

         

nanami7

3:04 am on Sep 9, 2004 (gmt 0)

10+ Year Member



I tried to use JavaScript conditional statement to hide the link to page1.asp if the field1 value is empty, somehow, nothing happens. Please help.

<script type="text/javascript">
var d = <%=(Form.Fields.Item("Field1").Value)%>

if ((d.length==0 ¦¦ (d==Null))
{
document.write(" ")
}
else
{
document.write("<li><a href="page1.asp?<%= "ID=" + Form.Fields.Item("ID").Value %>" onclick="NewWindow(this.href,'newpage','600','500','yes', 'center');return false" onfocus="this.blur()">newpage</a></li>")
}
</script>

Bernard Marx

7:26 am on Sep 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good morning,

It would be easier for us if you posted the actual output of the server script - ie: the client-side code (or an example thereof), otherwise we're a little on the dark.

There is probably quoting needed for the value (else it arrives looking like a variable). Perhaps this will do:

var d = [blue]"[/blue]<%=(Form.Fields.Item("Field1").Value)%>[blue]"[/blue]

Javascript is case-sensitive:

Null --> [blue]null[/blue]

I have a feeling it won't be specifically null anyway. Do you mean that the ASP might write "Null". In that case, you'll need the quotes again.

The nested quoting on the document.write statement is all messed up.
Start and end the statement with single quotes. Use double quotes for attributes then use \-escaped quotes (sgl or dbl for) for quoted arguments inside attribute values.

nanami7

2:27 am on Sep 10, 2004 (gmt 0)

10+ Year Member



Do you mean \ or \-?
Is it something like these?

<script type="text/javascript">
var d = "<%=(Form.Fields.Item("Field1").Value)%>"

if ((d.length==0 ¦¦ (d==null))
{
document.write(' ')
}
else
{
document.write('<li><a href="page1.asp?<%= "ID=" + Form.Fields.Item(\"ID\").Value %>" onclick="NewWindow(this.href,\'newpage\',\'600\',\'500\',\'yes\', \'center\');return false" onfocus="this.blur()">newpage</a></li>')
}
</script>

The result page shows:

Microsoft JScript compilation error '800a03f6'

Invalid character

for (\"ID\")

If I change the \" with ", then the page is normal except that they don't show the <li></li> despite the condition is met (d!= null).

Bernard Marx

8:44 am on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post the code that is received by the browser.
Not the ASP instructions. We can't really tell what's wrong until we know what the browser is getting.

There is no need to escape the quoting on the ASP (as far as JS is concerned. Client-side Javascript will never see it).

if ((d.length==0 ¦¦ (d==null))

d won't be null. It will hopefully be "" or "something"

However, if it were ever null (for some reason), then the order of the conditions should be changed, since comparing null.length==0 may not be a good idea.

if ( d==null ¦¦ d.length==0) 

I've just noticed that you have a bracket imbalance in your condition, which will definitely cause an error.

nanami7

3:18 am on Sep 13, 2004 (gmt 0)

10+ Year Member



when I changed it to:

if ( d=="" ¦¦ d.length==0) and inserted escape clauses in the open new window, i.e. onclick="NewWindow(this.href,\'extra\',\'600\',\'500\',\'yes\', \'center\');return false"

it works now. thanks for your help! :)