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") ?
I don't actually have a mac with which to test this, but I'm informed by a colleague that one of my scripts has failed in IE on the Mac; endless twiddling seems to point to the ternary operator as culprit.
I asked him to check that javascript:alert(true?"Yes":"No"); gave an alert when pasted into the address bar and he's reported that it does not. javascript:alert("someString"); works quite happily.
Can anyone confirm this bug or help narrow it to a single version? It seems a fairly large bug to have gone so under-reported, gooooogling for "mac ie ternary operator" doesn't return anything helpful.
I have a for loop and would like to increment a variable for (let's say) 2 instead of one (++). How can I do that?
I have tried for instance x + 2 instead of x++ but when I try it in IE an error saying that a script is making IE to run slowly and then nothing happens. So how to do this?
We all know that feature detection technique works from very beggining of user-agents supporting JavaScript. We can alway check if eg. document has a write property or smth else:
if(document.write) { }
We could do that for all of objects, so i have been surprised when i found 'in' operator, which for above example would be used like this:
if("write" in document) { }
So i have questioned myself what for ? I can always do:
if(document["write"]) { }
or even var prop = [...]; if(document[prop]) { }
which is supported better then 'in' operator ('in' is supported since W. IE 5.5 and NN 6 - around 2000 year). I can only guess that 'in' was provided in W. IE 5.5 only for convenience... but that is my guess and someone more knowlegable could write here some more opinions on this operator...
How you might achieve the following without using eval()? I've come up a bit short: function addOrSubtract(operator) { var a = 10; var b = 5; var answer = eval(a + operator + b); alert(answer); } addOrSubtract('+') // alerts 15 addOrSubtract('-') // alerts 5
I am trying to run some code that will check if the user enters two values from 'depart' and 'arrival' select lists that would make up an invalid journey: var cdate, ctime, cdepart, carrive, cname; with(window.document.example) {
It's well know that the ternary operator syntax is something like so: Code: (test) ? true doThis : false doThat Suppose that in case condition is false we want do nothing. How to code "do nothing"? May I let it blank or there are an appropriate syntax?
What kind of logical error am I making? I want the alert(); to execute if both of the variables (cjob and czip) are blank, but the only way I can get it to work is if I replace && with ||.
anotherVar should be -1 if there are no matches, but it doesn't seem to work var string = "abadafa"; var matches = string.match(/a/gi); //doesn't work with /z/ for example
var anotherVar = matches.length || -1; The default operator should return the second value if the first is null or false, and according to try { alert(matches.length);
I have a set of regular expressions that make heavy use of the | operator on sections that I do not really need to extract a match from. For example:
Code: var regexp = /([A-Z][A-Za-z]+) (jumps( high)?|leaps|bounds) over ([A-Z][A-Za-z]+) and (grabs|snags|gets) (a|an|the) (apple|orange|pear|grapes)/
The important part for extracting from the match array after using regexp.exec() are the names (the ([A-Z][A-Za-z]+) parts), I don't care which of the other things are matched. The problem is that using the | operator seems to necessitate using the () and adding a term to the match. This makes it difficult to know which term in the array will be the names, especially after editing the middle.So I'd like to be able to use the | operator on words and phrases without adding terms to the match array.
I'm trying to do something, but I don't know if it's possible. Basically, I want to have a public static class method that could access a private object's method. I would like to be able to do :
Class.method(InstanceOfClass);
The method would then access a private function from Class by doing something like
function method(param) { param.privateMethodOfClass(); }
I've done a lot research and experimentations but just can't come up with a solution... I don't even know if what I'm trying to do is possible.
Why is the callwhy is the slice method only a method of an Array instance? The reason why I ask is because if you want to use it for the arguments property of function object, or a string, or an object, or a number instance, you are forced to use Array .prototype slice.call(). And by doing that, you can pass in any type of object instance (Array, Number, String, Object) into it. So why not just default it as a method of all object instances built into the language?In other words, instead of doing this:
function Core(){ var obj = {a : 'a', b : 'b'}; var num = 1;[code]....
//right now none of the above would work but it's more convenient than using the call alternative.
} Core('dom','event','ajax');
Why did the designers of the javascript scripting language make this decision?
I have two methods and I would like to call somename1 method from within somename2 method. I have tried several ways to do so however I keep getting "TypeError" or "RefernceError" I have tried several ways to reference but I am still unable. What am I doing wrong. I would think this would be easy to do.
Is the form below a valid method of changing the id of an XHTML element, specifically the one actually being referenced? It does not seem to work for me.
function doSomething () { var obj = new Obj(); obj.foo = bar; doSomethingElse(obj); }
In function doSomethingElse I want to create the following line of DHTML
<div return true;">
What I want is for onClick to be defined to be the execution of the foo method of obj.
I tried document.writeln ("<div + obj.foo + "(); return true;'>");
What I get is the source code for bar stuck into the middle of the string. So how do I get just the name of the function bar in the string? Or ultimately, how do I get the handler to be bar in the DHTML?
While submiting the form elements, if the GET method is used in the <form>tag, the data is submitted as a query string.I think there should be restriction in size of data submitted by the browser IE. Does any body know the exact details of it?
I only use forms as input and output to javascripts. Many times I use innerhtml to write to a <div> for output. I don't know of an alternative for input.
I sometimes have problems if I use method="post" and just leave it off. The browser tried to reload the page after the script ran. Sometimes I am able to even leave off <form> and </form>. Could someone explain when form and method are actually needed?
I responded in comp.lang.javascript to "Adding to fields with onchange". I had to leave "method=" out to get it to work without trying to reload the page.