By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,946 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,946 IT Pros & Developers. It's quick & easy.

new operator as a method

P: n/a
Is there any way to call the new operator as a method (function)? The
reason is that I've got IE as a COM object (Imagine I've brought up IE
using VB) and it's easy to call every method of any DOM element
(including window). But if I want to create a new object, then it's
more complicated. Of course I could always execute js code (using
window.execScript) which will create the object and save it as a
variable on the window object and then pick it up from the COM creator,
but really...

Consider the following page snippet which nicely adds an option to the
empty select element. Of course, I could use the W3C createElement,
addChild, muckWithDOM approach to avoid the execScript, but both of
these are going to add huge amounts of time and substantial complexity
to an otherwise one liner:

<form method=pos action=''>
<select name=sel id=sel></select>
<script type='text/javascript'>
var sel=document.getElementById('sel');
sel.options[0] = new window.Option("foo", "bar");
</script>
</form>

Can't I do something like
window.Option.newInstance("foo", "bar")
in place of the
new window.Option("foo", "bar") ?

Thanks,
Csaba Gabor from Vienna

Aug 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
VK

Csaba Gabor wrote:
Is there any way to call the new operator as a method (function)?


I read this question several times: together with your explanations.
I couldn't find any sense in neither of both.

As The Cabal is also silent, I guess they are experiencing the same
problem.

The "new" operator used to call a constructor to get an instance of an
object. Such constructor in its own turn can have methods inside using
the "new" operator.

The only one more thing I can say: if you find execScript() so
advandageous, you should use the proper type declaration in your
script:

<script type="text/Jscript">
(see type specs at
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/type_8.asp>)

At least FF will not be hiccing of it.


P.S. Whoever doesn't believe in accosiative array in JavaScript : "The
Cabal" is not an insultation. It's from the newsgroups' history.
<http://www.livinginternet.com>

Aug 13 '05 #2

P: n/a
VK wrote:
Csaba Gabor wrote:
Is there any way to call the new operator as a method (function)?
I read this question several times: together with your explanations.
I couldn't find any sense in neither of both.


Let me try one more way. Imagine in VBScript...
Set IE=CreateObject("InternetExplorer.Application")
IE.visible = true
IE.navigate "about:blank"
IE.document.body.innerHTML = _
"<form method=pos action=''>" &
"<select name=sel id=sel></select></script></form>"

At this point I can get at the script element with:
IE.document.getElementById('sel')
and similarly for pretty much any DOM object. So, I
also have access to the corresponding methods of those DOM objects.
The problem is, new is not a method, even though it works by
creating an object (a new instance), placing certain properties,
and then running a certain initiation function, then returning
the object (more or less). It seems reasonable to want this
functionality as a function, that is to say method, that I can
access.
As The Cabal is also silent, I guess they are experiencing the same
problem.
Or they are out partying. It's Saturday night.
The "new" operator used to call a constructor to get an instance of an
object. Such constructor in its own turn can have methods inside using
the "new" operator.

The only one more thing I can say: if you find execScript() so
advandageous, you should use the proper type declaration in your
script:

<script type="text/Jscript">


That's not really relevant to execScript, is it, since it has its own
context that it runs in? - but if it was relevant, the documentation
shows that text/javascript is another way of saying JScript (as far a
Microsoft is concerned), but staying with text/javascript makes sense
because other browsers understand that. In fact, execScript has it's
own language parameter in the second argument which defaults to
Jscript. And this is documented at:

http://msdn.microsoft.com/workshop/a...execscript.asp

However, the whole point of this post was to avoid using execScript.

Also, FF is not immune here. There is supposed to be a COM object that
allows access, and the group's claim is that they want to allow
everything that the 'IE COM object' allows. I haven't checked it out
yet, but I am planning to in the next month or so. The web site is:
http://www.iol.ie/~locka/mozilla/mozilla.htm

Csaba Gabor from Vienna

Aug 13 '05 #3

P: n/a
Csaba Gabor wrote:
VK wrote:
Csaba Gabor wrote:
Is there any way to call the new operator as a method (function)?


I read this question several times: together with your explanations.
I couldn't find any sense in neither of both.

Let me try one more way. Imagine in VBScript...
Set IE=CreateObject("InternetExplorer.Application")
IE.visible = true
IE.navigate "about:blank"
IE.document.body.innerHTML = _
"<form method=pos action=''>" &


OK, if it's that easy, to get into the document from IE, you just use
DOM calls to create new elements in the document. You want all kinds of
custom constructors which create and add <options> (and all the other
possible HTML elements) and add them into the document... *where* should
they add them? These functions don't exist, so write them yourself in
javascript. For example, have the following function loaded into your
page in the initial page load using the <script> tag.

function addOption(selID, value, text)
{
var sel = document.getElementById(selID);
var opt = document.createElement("option");
opt.value = value;
opt.appendChild(document.createTextNode(text));
}

Then call that from your VB. I'm not sure how you do that, I don't know
VB, or the object model that it offers you when you have IE as a COM
object, but you should be able to execute javascript functions in the IE
window???

IE.document.window.execScript("addOption('sel', 1, 'foo');")

Something like that.

Aug 14 '05 #4

P: n/a


Csaba Gabor wrote:

Consider the following page snippet which nicely adds an option to the
empty select element. Of course, I could use the W3C createElement,
addChild, muckWithDOM approach to avoid the execScript, but both of
these are going to add huge amounts of time and substantial complexity
to an otherwise one liner:

<form method=pos action=''>
<select name=sel id=sel></select>
<script type='text/javascript'>
var sel=document.getElementById('sel');
sel.options[0] = new window.Option("foo", "bar");
</script>
</form>

Can't I do something like
window.Option.newInstance("foo", "bar")
in place of the
new window.Option("foo", "bar") ?


I have tested with IE 6, there you can simply call
var option = Option('text', 'value');
and a new option element object with text and value is created so if all
you want to do is use Option that way from VB embedding IE then it
should work.
For a general solution you will need to use the DOM factory methods even
if it is some more statements you need to write.
ECMAScript usually spells out exactly what results
ConstructorFunction()
compared to
new ConstructorFunction()
has, sometimes you get the same result, but of course only for the core
objects ECMAScript defines.
And for host specific stuff like Option you will then have to test what
the host does when you try to simply call. Mozilla for instance will
give you an error for a simple Option() call.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 14 '05 #5

P: n/a
Martin Honnen wrote:
Csaba Gabor wrote:
Consider the following page snippet which nicely adds an option to the

<form method=pos action=''>
<select name=sel id=sel></select>
<script type='text/javascript'>
var sel=document.getElementById('sel');
sel.options[0] = new window.Option("foo", "bar");
</script>
</form>

I have tested with IE 6, there you can simply call
var option = Option('text', 'value');


Wow Martin, thanks for your reply and that egg. I was so sure that I
had tested this with IE before posting that on the first two readings
of your reply, I read your above line to have a 'new' in it. It was
only then that I saw you telling me that I can do what I want with
Option just the way I wanted. Many thanks for that. In retrospect, I
see that I tested with FF.

Speaking of which... FF does let you do new
window.Option('text','value'), but if you omit the new it complains
about Option not being a function

So, I can use my one liner from VB/Script:
Set mySel = IE.document.getElementById('sel')
Set options = mySel.options 'less dereferencing for loops
options.add IE.document.parentWindow.Option('text','value')

ExG, in the previous case it would have been something close to:
'Stuff a function into the current browser document
IE.document.parentWindow.execScript( _
"function newOpt(text,val) {return (new Option(text,val));}")
....
Set mySel = IE.document.getElementById('sel')
Set options = mySel.options 'less dereferencing for loops
'Now we can call to the function we defined above
options.add IE.document.parentWindow.newOpt('text','value')

which is practically the same thing only I didn't think it was in the
spirit of that DOM COM to hang a function into the browser. But
ultimately, that is what my approach will be if I expand to FF or other
objects.

Csaba Gabor

Aug 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.