new operator as a method

C

Csaba Gabor

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
 
V

VK

Csaba said:
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/d.../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>
 
C

Csaba Gabor

VK said:
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/author/dhtml/reference/methods/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
 
E

ExGuardianReader

Csaba said:
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.
 
M

Martin Honnen

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.
 
C

Csaba Gabor

Martin said:
Csaba said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top