JavaScript: refering to the default object when using with()

  • Mirkoo
  • Born
  • Born
  • Mirkoo
  • Posts: 2

Post 3+ Months Ago

Hello,

can anybody tell me how I can refer to the default object in the with-statement? Is there an expression or statement or something?

I have figured out a workaround, but it's not really elegant. I will post it to point out my problem more clearer:

<pre>
for (n=0; n < document.links.length; n++)
with(document.links[n]) {
alert('Default object in with-Statement is: '+ childNodes[0].parentNode); // this shows the original object (in fact, the URL)
...
}
</pre>

Of course this is not "the right way", but it works I guess (though I'm not sure, if every link has childNodes - but then, I want a general solution, working for any object).

I hope you understand the problem, since I'm not a native-english speaker ;)

Mirkoo
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

I have no idea about a with statement, but try

Code: [ Select ]
this


however, I would tend to just use variables:
Code: [ Select ]
for (n=0; n < document.links.length; n++){
  var currentLink = document.links[n];
  alert(currentLink.href);

}
  1. for (n=0; n < document.links.length; n++){
  2.   var currentLink = document.links[n];
  3.   alert(currentLink.href);
  4. }


I have used with in Visual BASIC and always found it to be very clumsy. The fact that javascript does not enforce strict typing makes it really easy to just use variables for this type of work.

Plus you can have multiple object variables. Using multiple with's just gets messy.

Hope this helps :D
  • Mirkoo
  • Born
  • Born
  • Mirkoo
  • Posts: 2

Post 3+ Months Ago

I have tried "this" already - doesn't work.

But using variables is a good idea, thanks. I've already done so in other cases, don't know why I didn't do so here. Seems very handy and useful.

Mirkoo

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.