Why is this not working?

Discussion in 'Javascript' started by Mikhail Kovalev, Apr 6, 2007.

  1. 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
    Mikhail Kovalev, Apr 6, 2007
    #1
    1. Advertising

  2. Mikhail Kovalev

    Guest

    On Apr 6, 2:37 pm, "Mikhail Kovalev" <> wrote:
    > 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
    , Apr 6, 2007
    #2
    1. Advertising

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


    On 7 Apr, 00:55, Randy Webb <> wrote:
    > said the following on 4/6/2007 2:59 PM:
    >
    >
    >
    > > On Apr 6, 2:37 pm, "Mikhail Kovalev" <> wrote:
    > >> 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/
    Mikhail Kovalev, Apr 7, 2007
    #3
  4. Mikhail Kovalev

    -Lost Guest

    "Randy Webb" <> wrote in message
    news:...

    > 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
    -Lost, Apr 7, 2007
    #4
  5. Mikhail Kovalev

    Evertjan. Guest

    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?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 8, 2007
    #5
  6. Mikhail Kovalev

    Kevin Guest

    On Apr 7, 6:16 pm, Randy Webb <> wrote:

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

    --
    - Kevin
    Kevin, Apr 9, 2007
    #6
  7. In comp.lang.javascript message <
    legroups.com>, Mon, 9 Apr 2007 09:53:34, Kevin <>
    posted:
    >On Apr 7, 6:16 pm, Randy Webb <> wrote:
    >
    >> 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.


    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

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Apr 9, 2007
    #7
  8. Mikhail Kovalev

    Bart Lateur Guest

    Evertjan. wrote:

    >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);


    --
    Bart.
    Bart Lateur, Apr 10, 2007
    #8
  9. Mikhail Kovalev

    Bart Lateur Guest

    Randy Webb wrote:

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

    --
    Bart.
    Bart Lateur, Apr 10, 2007
    #9
  10. Mikhail Kovalev

    Evertjan. Guest

    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.



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 10, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1bA==?=

    not sure why close window function not working

    =?Utf-8?B?UGF1bA==?=, Nov 23, 2004, in forum: ASP .Net
    Replies:
    14
    Views:
    1,151
    =?Utf-8?B?UGF1bA==?=
    Nov 29, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    438
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    838
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,661
    Smokey Grindel
    Dec 2, 2006
  5. Skybuck Flying
    Replies:
    16
    Views:
    642
    tragomaskhalos
    Aug 25, 2007
Loading...

Share This Page