Why is this not working?

  • Thread starter Mikhail Kovalev
  • Start date
M

Mikhail Kovalev

opens a new window window_1 which contains a form, uses window
reference to change values there and submits, anyone can have a quick
glimse over this
windows opens ok, nothing happens afterwards : (

var window_1; // global value

function function_1() {
window_1 = window.open('http//...');
setTimeout(function_2, 10000); // 10 secs to load
}

function function_2()
{
window_1.forms['postform'].elements['commenttext'].value = 'test';
window_1.forms['postform'].submit();
}

function_1 is called in body onload in the opened window
 
B

brunascle.maps

opens a new window window_1 which contains a form, uses window
reference to change values there and submits, anyone can have a quick
glimse over this
windows opens ok, nothing happens afterwards : (

var window_1; // global value

function function_1() {
window_1 = window.open('http//...');
setTimeout(function_2, 10000); // 10 secs to load

}

function function_2()
{
window_1.forms['postform'].elements['commenttext'].value = 'test';
window_1.forms['postform'].submit();

}

function_1 is called in body onload in the opened window


i think when you're doing setTimeout() you have to put the code in
quotes, so it should be:
setTimeout("function_2()", 10000);

and in function_2 i think you want to do winow_1.document.forms rather
than window_1.forms
 
M

Mikhail Kovalev

Thanks guys, anyway I figured it out , the script doesnt work bc of
cross site scripting restrictons
The new window Im opening is on another domain

However it tried a similar method with same site in two-split frame
window, and it works in Safari 1.3.2

http://pastebin.se/12827

Could be a security flaw in safari


(e-mail address removed) said the following on 4/6/2007 2:59 PM:


opens a new window window_1 which contains a form, uses window
reference to change values there and submits, anyone can have a quick
glimse over this
windows opens ok, nothing happens afterwards : (
var window_1; // global value
function function_1() {
window_1 = window.open('http//...');
setTimeout(function_2, 10000); // 10 secs to load
}
function function_2()
{
window_1.forms['postform'].elements['commenttext'].value = 'test';
window_1.forms['postform'].submit();
}
function_1 is called in body onload in the opened window
i think when you're doing setTimeout() you have to put the code in
quotes, so it should be:
setTimeout("function_2()", 10000);

It is very easy to test whether it should be quoted or not. Try this
code in the browser of your choice:

window.onload=testIt;
function testIt(){
window.setTimeout(myFunction,1000)
window.setTimeout('myFunction2()',2000)}

function myFunction(){
alert('no quotes used')}

function myFunction2(){
alert('quotes used')

}
and in function_2 i think you want to do winow_1.document.forms rather
than window_1.forms

Possibly, but window_1.document.forms might work better than winow_1..

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
 
L

-Lost

It is very easy to test whether it should be quoted or not. Try this code in the browser
of your choice:


window.onload=testIt;
function testIt(){
window.setTimeout(myFunction,1000)
window.setTimeout('myFunction2()',2000)
}
function myFunction(){
alert('no quotes used')
}
function myFunction2(){
alert('quotes used')
}

Alright, Mr. Webb. Please explain how this tells me whether or not it should be quoted or
not?

I threw that in Firefox (because I thought both would work) and both worked. What did I
miss?

Knowing me, I am sure I missed something reaaally simple. I just love it when stuff goes
over my head. Hehe.

-Lost
 
E

Evertjan.

Randy Webb wrote on 08 apr 2007 in comp.lang.javascript:
Perhaps two more lines of code will show it more obviously:

Try this code instead, it has better alerts:

function myFunction1(){alert('no quotes used without ()')}
function myFunction2(){alert('quotes used with ()')}
function myFunction3(){alert('quotes used without ()')}
function myFunction4(){alert('no quotes used with ()')}

window.onload=testIt;
function testIt(){
window.setTimeout(myFunction1,5000)
window.setTimeout('myFunction2()',5000)
window.setTimeout('myFunction3',5000)
window.setTimeout(myFunction4(),5000)
}

The first one will work, no quotes without ().
The second one will alert.
The third one will never alert. FF gives the error "useless settimeout
call" The fourth one will alert instantly.

Simple rule? If you quote it, use (). If you don't quote it, don't use
(). If you use () without quotes then the function executes instantly.

Nice.

Could you expand this with the restrictiona on using function parameters,
like:

var that='what'
window.setTimeout('myFunction99('+this+',that)',5000)

, and perhaps we can make it a complete faqentry?
 
K

Kevin

Simple rule? If you quote it, use (). If you don't quote it, don't use
(). If you use () without quotes then the function executes instantly.


It might help to also mention the reason why this is. If you pass a
string to setTimeout(), JavaScript will eval() it. If you pass a
function object (such as the name of the function without () after
it), JavaScript will call() it.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
It might help to also mention the reason why this is. If you pass a
string to setTimeout(), JavaScript will eval() it. If you pass a
function object (such as the name of the function without () after
it), JavaScript will call() it.

ISTM that there is more to say.

The following statements may not themselves be exactly correct, but they
should indicate what needs to be considered in providing a complete
description of the result of a timeout.

If a string is given, it is the content of the string when setTimeout is
called that is passed, and the content is held until the timeout fires
and then evaluated (it often turns out to be a function call, and can
include arguments).

If a function object is given, then whatever that object is when the
timeout fires is called, with no arguments.

If a function call is given, it will be evaluated when setTimeout is
called; if it returns a function or string they will be treated as above
and anything else will be converted to String ... - I think.

A full description must allow for code such as follows, which some may
expect to give 1 and others to give 2.

function FF() { alert(1) }
function GG() { alert(2) }
AA = FF
X = setTimeout(AA, 100)
FF = GG
 
B

Bart Lateur

Evertjan. said:
Could you expand this with the restrictiona on using function parameters,
like:

var that='what'
window.setTimeout('myFunction99('+this+',that)',5000)

Won't work, "this" generally won't be turned into a useful string. You'd
better use a variable. Or make this a closure.

And "this" will have a different value when the function gets called, so
you better save it into a variable. Heh. Well, in the latter case, it
can be a local variable, in the former it needs to be global.

var obj = this;
var that='what';
window.setTimeout('myFunction99(obj,that)',5000)

or

// obj and that may be local variables to the current function:
window.setTimeout(function() { myFunction99(obj,that) },5000);
 
B

Bart Lateur

Randy said:
The third one will never alert. FF gives the error "useless settimeout call"

That's because a bare myFunction3 returns the reference to the function,
instead of calling it.
 
E

Evertjan.

Bart Lateur wrote on 10 apr 2007 in comp.lang.javascript:
var obj = this;
var that='what';
window.setTimeout('myFunction99(obj,that)',5000)

Are you sure the above is a safe alternative?

var that = 'what';
window.setTimeout('alert(that)',5000);
that = 'blah';

Meseems that after 5 seconds,
the alert function will see and alert
'blah' as the parameter 'that'-'s value.

..... Tested it, and so it does.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top