Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

.

...........RESOLVED (see next post)

.

I'm looking into somebody's Javascript code and I came across this syntax, I have no idea what the if/with line means and Google isn't helping. BTW, this is part of a web control that puts a HELP link on the top right of pages.

 

<div id="HelpTopRight" class="HelpTopRight" style="white-space:nowrap;">
<xsp:HelpLink ID="lnk" runat="server" />
</div>
<script type="text/javascript" language="javascript">
var x=document.getElementById('HelpTopRight')
[b]if(x)with(x)[/b]
{
	style.setExpression('left',document.body.clientWidth-60)
}
</script>

Edited by travisowens
Experience is something you don't get until just after the moment you needed it
Posted (edited)

I wouldn't have questioned this at all if I had seen a more formal syntax, such as

if(x)
{
with(x)
{
	style.setExpression('left',document.body.clientWidth-60)
}
}

 

I'm outright shocked that with() works outside the brackets.

Edited by travisowens
Experience is something you don't get until just after the moment you needed it
Posted

Brackets

 

Rather than it being a case of with() being outside the brackets, it is a case of the brackets belonging to the with block rather than the if block. Any control structure can be used without brackets - operating on a single statement. For example:

 

if (x)
   doThis();

 

However, that single statement can be another control structure with its own block of code (as in this case).

 

:)

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...