When is window.location = "url" executed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roland

    #1

    When is window.location = "url" executed?

    Hello,

    the example code is the following(the number in parentheses at the
    beginning are just for reference)(The complete HTML file is at the end
    of this article):

    (1)window.locat ion = 'http://www.google.com' ;
    (2)alert("I'm still here!");
    (3)window.open( "http://www.slashdot.co m", "_blank");

    What happens, is that after the assignment (1) theoretically the rest
    of the code shouldn't execute, since the window should be loading a
    new URL. But in my browser(Firefox 1.0 on GNU/Linux) the browser also
    executes (2) and (3).
    It seems that the window is only loaded with the new content after the
    script has finished executing.
    So is this behaviour wrong or correct?

    In all documentations I searched there is no mention of WHEN the new
    location should be loaded, if immediately or only after the script
    finished executing.

    Can anyone try this with IE?

    Thank you

    Roland

    Complete file:

    <html:html>
    <head>
    <script type="text/javascript">
    window.location = 'http://www.google.com' ;
    alert("I'm still here!");
    window.open("ht tp://www.slashdot.co m", "_blank");
    </script>
    </head>
    <body>
    Test.
    </body>
    </html:html>
  • Richard Cornford

    #2
    Re: When is window.location = &quot;url&qu ot; executed?

    Roland wrote:
    <snip>[color=blue]
    > (1)window.locat ion = 'http://www.google.com' ;
    > (2)alert("I'm still here!");
    > (3)window.open( "http://www.slashdot.co m", "_blank");
    >
    > What happens, is that after the assignment (1) theoretically
    > the rest of the code shouldn't execute, since the window
    > should be loading a new URL.[/color]

    If your expectation is that the assignment to the location property is
    the last operation then why is there code after it?

    But no, the assignment to the location property does not replace the
    page immediately, it might (probably does) make an HTTP request
    immediately, that may ultimately return a response that will result in
    the page being replaced. But that would be an asynchronous process
    anyway so there is not reason to expect the script to terminate at the
    point of the assignment.
    [color=blue]
    > But in my browser(Firefox 1.0 on GNU/Linux) the browser
    > also executes (2) and (3).
    > It seems that the window is only loaded with the new content
    > after the script has finished executing.[/color]

    Most likely the page is only replaced when its replacement arrives from
    the server.
    [color=blue]
    > So is this behaviour wrong or correct?[/color]

    No specification defines behaviour for assignments to the location
    object to the extent that any single behaviour could be expected.
    [color=blue]
    > In all documentations I searched there is no mention of
    > WHEN the new location should be loaded, if immediately
    > or only after the script finished executing.[/color]

    It would be unrealistic to expect the page to be replaced prior to the
    response to the HTTP request, and no reason to expect the HTTP
    request/response process to confine, or be confined by, the execution of
    scripts.
    [color=blue]
    > Can anyone try this with IE?[/color]
    <snip>

    To what end? If you don't want script to execute after the assignment to
    the location property then don't put code after the assignment.

    Richard.


    Comment

    • askMe

      #3
      Re: When is window.location = &quot;url&qu ot; executed?

      You should do an IF with a BODY ONLOAD, but a simple EXIT, as shown
      below, ought to answer the question.

      <html>
      <head>
      <script type="text/javascript">
      window.location = 'http://www.askblax.com ';
      exit(); // stage left
      alert("Never gonna reach me!");
      window.open("ht tp://www.google.com" , "_blank");

      </script>
      </head>
      <body bgcolor=pink>
      Without a valid test before, I will not redirect pages this
      way.<BR><BR>Wit hout a valid test before, I will not redirect pages this
      way. The only way to reach this line is if a javascript error occurs
      before the exit command.
      </body>
      </html>



      Comment

      • askMe

        #4
        Re: When is window.location = &quot;url&qu ot; executed?

        >> Most likely the page is only replaced when its replacement arrives
        from
        the server. <<

        I think you are right.
        [color=blue][color=green]
        >> To what end? If you don't want script to execute after the[/color][/color]
        assignment to
        the location property then don't put code after the assignment. <<

        Frames maybe.

        Comment

        • Evertjan.

          #5
          Re: When is window.location = &quot;url&qu ot; executed?

          askMe wrote on 04 mei 2005 in comp.lang.javas cript:
          [color=blue]
          > You should do an IF with a BODY ONLOAD, but a simple EXIT, as shown
          > below, ought to answer the question.
          >
          > <html>
          > <head>
          > <script type="text/javascript">
          > window.location = 'http://www.askblax.com ';
          > exit(); // stage left[/color]

          exit()?

          What language is that, I askYou?

          [color=blue]
          > alert("Never gonna reach me!");
          > window.open("ht tp://www.google.com" , "_blank");
          >
          > </script>
          >[/color]



          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Lee

            #6
            Re: When is window.location = &quot;url&qu ot; executed?

            askMe said:[color=blue]
            >[color=green][color=darkred]
            >>> Most likely the page is only replaced when its replacement arrives[/color][/color]
            >from
            >the server. <<
            >
            >I think you are right.
            >[color=green][color=darkred]
            >>> To what end? If you don't want script to execute after the[/color][/color]
            >assignment to
            >the location property then don't put code after the assignment. <<
            >
            >Frames maybe.[/color]

            What about frames? That response doesn't seem to make any sense.
            The assignment to the location property is going to clear the
            rest of the code (sooner or later), anyway.

            Comment

            • askMe

              #7
              Re: When is window.location = &quot;url&qu ot; executed?


              Lee wrote:[color=blue]
              > askMe said:[color=green]
              > >[color=darkred]
              > >>> Most likely the page is only replaced when its replacement[/color][/color][/color]
              arrives[color=blue][color=green]
              > >from
              > >the server. <<
              > >
              > >I think you are right.
              > >[color=darkred]
              > >>> To what end? If you don't want script to execute after the[/color]
              > >assignment to
              > >the location property then don't put code after the assignment. <<
              > >
              > >Frames maybe.[/color]
              >
              > What about frames? That response doesn't seem to make any sense.
              > The assignment to the location property is going to clear the
              > rest of the code (sooner or later), anyway.[/color]

              Your question was "to what end?"...

              If I have a frame set of two pages and someone calls one of the pages
              on its own, a test could be done from the page to see the current
              parent/top window. If the result is not what is expected, redirect to
              the parent/top to remake the frameset, else exit the javascript and
              continue loading the current page because the page is already loading
              as designed.

              Expect the unexpected.

              Comment

              • askMe

                #8
                Re: When is window.location = &quot;url&qu ot; executed?

                It is Javascript, I tellYou. :-)



                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: When is window.location = &quot;url&qu ot; executed?

                  askMe wrote:
                  [color=blue]
                  > If I have a frame set of two pages and someone calls one of the pages
                  > on its own, a test could be done from the page to see the current
                  > parent/top window. If the result is not what is expected, redirect to
                  > the parent/top to remake the frameset,[/color]

                  Child's play. Within the frame document:

                  <head>
                  ...
                  <script type="text/javascript">
                  /**
                  * @author
                  * (C) 2003, 2004 Thomas Lahn &lt;types.js@Po intedEars.de&gt ;
                  * Distributed under the GNU GPL v2 and above.
                  * @optional Object|string o
                  * Object to be determined an method, i.e. a
                  * <code>Functio n</code> object assigned as property of
                  * another object. May also be a string to be evaluated
                  * and so is applicable to unknown properties.
                  * @return type boolean
                  * <code>true</code> if <code>o</code> is a method,
                  * <code>false</code> otherwise.
                  * @see #isMethodType()
                  */
                  function isMethod(m)
                  {
                  var t;
                  (m = eval(m)) && (t = typeof m);
                  return (t == "function" || t == "object");
                  }

                  if (typeof parent != "undefined"
                  && typeof top != "undefined"
                  && parent == top // not in a frameset
                  && typeof location != "undefined" )
                  {
                  var s = "my_frameset.ht ml?" + encodeURICompon ent(location);
                  if (isMethod("top. location.replac e"))
                  {
                  top.location.re place(s);
                  }
                  else
                  {
                  top.location = s;
                  }
                  }
                  </script>
                  ...
                  </head>

                  Within the frameset document:

                  <head>
                  ...
                  <meta http-equiv="Content-Script-Type" content="text/javascript">
                  ...
                  </head>

                  <frameset ... onload="if (location.searc h) frames[...].location =
                  decodeURICompon ent(location.se arch.replace(/^\??(.*)/, '$1'));" ...>
                  ...
                  </frameset>
                  [color=blue]
                  > else exit the javascript[/color]

                  A client-side script in a UA environment cannot be exited. It can simply
                  be not further executed, i.e. if no statement or expression follows (or
                  the current statement results in a fatal error), the script ends.
                  [color=blue]
                  > and continue loading the current page because the page is already loading
                  > as designed.[/color]

                  See above. Redirection will not be performed if the document is part
                  of a frameset.
                  [color=blue]
                  > Expect the unexpected.[/color]

                  Learn how to use search engines.


                  PointedEars

                  Comment

                  • askMe

                    #10
                    Re: When is window.location = &quot;url&qu ot; executed?


                    Thomas 'PointedEars' Lahn wrote:[color=blue]
                    > askMe wrote:
                    >[color=green]
                    > > If I have a frame set of two pages and someone calls one of the[/color][/color]
                    pages[color=blue][color=green]
                    > > on its own, a test could be done from the page to see the current
                    > > parent/top window. If the result is not what is expected, redirect[/color][/color]
                    to[color=blue][color=green]
                    > > the parent/top to remake the frameset,[/color]
                    >
                    > Child's play. Within the frame document:
                    >[/color]

                    Actually, manipulating frames is a pain in the. . . That is why I don't
                    use them if I don't have to.
                    [color=blue]
                    > <head>
                    > ...
                    > <script type="text/javascript">
                    > /**
                    > * @author
                    > * (C) 2003, 2004 Thomas Lahn[/color]
                    &lt;types.js@Po intedEars.de&gt ;[color=blue]
                    > * Distributed under the GNU GPL v2 and above.
                    > * @optional Object|string o
                    > * Object to be determined an method, i.e. a
                    > * <code>Functio n</code> object assigned as property of
                    > * another object. May also be a string to be evaluated
                    > * and so is applicable to unknown properties.
                    > * @return type boolean
                    > * <code>true</code> if <code>o</code> is a method,
                    > * <code>false</code> otherwise.
                    > * @see #isMethodType()
                    > */
                    > function isMethod(m)
                    > {
                    > var t;
                    > (m = eval(m)) && (t = typeof m);
                    > return (t == "function" || t == "object");
                    > }
                    >
                    > if (typeof parent != "undefined"
                    > && typeof top != "undefined"
                    > && parent == top // not in a frameset
                    > && typeof location != "undefined" )
                    > {
                    > var s = "my_frameset.ht ml?" + encodeURICompon ent(location);
                    > if (isMethod("top. location.replac e"))
                    > {
                    > top.location.re place(s);
                    > }
                    > else
                    > {
                    > top.location = s;
                    > }
                    > }
                    > </script>
                    > ...
                    > </head>
                    >
                    > Within the frameset document:
                    >
                    > <head>
                    > ...
                    > <meta http-equiv="Content-Script-Type" content="text/javascript">
                    > ...
                    > </head>
                    >
                    > <frameset ... onload="if (location.searc h) frames[...].location =
                    > decodeURICompon ent(location.se arch.replace(/^\??(.*)/, '$1'));" ...>
                    > ...
                    > </frameset>
                    >[color=green]
                    > > else exit the javascript[/color]
                    >
                    > A client-side script in a UA environment cannot be exited. It can[/color]
                    simply[color=blue]
                    > be not further executed,[/color]

                    Exiting is halting/aborting execution.
                    [color=blue]
                    > i.e. if no statement or expression follows (or
                    > the current statement results in a fatal error), the script ends.
                    >[/color]

                    It will exit that way, too, although that is not the best way to end a
                    script.
                    [color=blue][color=green]
                    > > and continue loading the current page because the page is already[/color][/color]
                    loading[color=blue][color=green]
                    > > as designed.[/color]
                    >
                    > See above. Redirection will not be performed if the document is part
                    > of a frameset.
                    >[/color]

                    Hmmm... I don't think that is true. I struggled with a piece of code
                    recently that required redirection of frames. At one point, it seemed
                    to work, but I never got any consistent behavior so I ended up
                    scrapping it. Also, it is possible to refresh the top frame even if
                    not the subordinates. Of course all the frames can be loaded/reloaded
                    in response to events, so no major loss if redirection doesn't work.
                    [color=blue][color=green]
                    > > Expect the unexpected.[/color]
                    >
                    > Learn how to use search engines.
                    >[/color]

                    Search to learn engine use why.


                    [color=blue]
                    >
                    > PointedEars[/color]

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: When is window.location = &quot;url&qu ot; executed?

                      askMe wrote:
                      [color=blue]
                      > Thomas 'PointedEars' Lahn wrote:[color=green]
                      >> askMe wrote:[color=darkred]
                      >> > If I have a frame set of two pages and someone calls one of the[/color][/color]
                      > pages[/color]
                      ^^^
                      *Please* either don't use Google Groups interface for posting or learn
                      how to post readable quotes.

                      [quoting repaired:]
                      [color=blue][color=green][color=darkred]
                      >> > on its own, a test could be done from the page to see the current
                      >> > parent/top window. If the result is not what is expected, redirect
                      >> > to the parent/top to remake the frameset,[/color]
                      >>
                      >> Child's play. Within the frame document:[/color]
                      >
                      > Actually, manipulating frames is a pain in the. . .[/color]

                      Why?
                      [color=blue]
                      > That is why I don't use them if I don't have to.[/color]

                      Your logic is erroneous. What if client-side scripting is not even
                      supported?

                      I don't use frames if there are other viable solutions because otherwise
                      it would reduce usability. Think of no-script UAs, smaller viewports,
                      bookmarks etc.
                      [color=blue][color=green]
                      >> [...][/color][/color]

                      It was completely unnecessary to quote all my code since you are not
                      directly referring to it. Please read the FAQ (Notes) and learn how
                      to post.
                      [color=blue][color=green][color=darkred]
                      >> > else exit the javascript[/color]
                      >>
                      >> A client-side script in a UA environment cannot be exited. It can
                      >> simply be not further executed,[/color]
                      >
                      > Exiting is halting/aborting execution.[/color]

                      Yes. Just what I wrote: A client-side script in a UA cannot be
                      exited, it can only be not further executed. This is different.
                      [color=blue][color=green]
                      >> i.e. if no statement or expression follows (or
                      >> the current statement results in a fatal error), the script ends.[/color]
                      >
                      > It will exit that way, too, although that is not the best way to end a
                      > script.[/color]

                      It is the only way in a client-side script in a UA. You could only wrap
                      all the global code in single global method, call that method from global
                      execution context and use the `return' statement to leave the local
                      execution context. But that would be not different to what I wrote:
                      Execution then stops because after the method call no statement follows
                      in global execution context.
                      [color=blue][color=green][color=darkred]
                      >> > and continue loading the current page because the page is already
                      >> > loading as designed.[/color]
                      >>
                      >> See above. Redirection will not be performed if the document is part
                      >> of a frameset.[/color]
                      >
                      > Hmmm... I don't think that is true.[/color]

                      Does not matter here what you may think. It will not be performed then
                      because parent != top; the condition will evaluate to `false', so the
                      `&&' operation will evaluate to `false' and so the parameter of the `if'
                      statement will evaluate to `false' which will cause the bytecode
                      interpreter to skip the compiled block that follows:

                      | [...]
                      | if (typeof parent != "undefined"
                      | && typeof top != "undefined"
                      | && parent == top // not in a frameset
                      | && typeof location != "undefined" )
                      | {
                      | // [redirection]
                      | }
                      | [no further statement or expression]
                      [color=blue]
                      > I struggled with a piece of code recently that required redirection of
                      > frames.[/color]

                      But not with my piece of code.
                      [color=blue]
                      > At one point, it seemed to work, but I never got any consistent behavior
                      > so I ended up scrapping it.[/color]

                      You may want to post it (or a link to it if it's too big) here for a more
                      competent evaluation.
                      [color=blue]
                      > Also, it is possible to refresh the top frame even if not the
                      > subordinates. Of course all the frames can be loaded/reloaded
                      > in response to events, so no major loss if redirection doesn't work.[/color]

                      I don't know what this has to do with the task discussed.
                      [color=blue][color=green][color=darkred]
                      >> > Expect the unexpected.[/color]
                      >> Learn how to use search engines.[/color]
                      >
                      > Search to learn engine use why.[/color]

                      Back your pardon?


                      PointedEars

                      Comment

                      Working...