window.location not working

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

    #1

    window.location not working

    I have a very simple confirm link on a website using the following code:

    function confirmNewSuppl ier(){
    if(confirm(' Are you sure that you can\'t select a supplier from the
    list? \n(Click OK to request a new supplier or Cancel to return to the
    Directory)')){
    window.location = 'l1stage1.php';
    }
    }


    the link is as follows:

    <a href="javascrip t:;" onClick="confir mNewSupplier()"
    class="mainText Normal">Creatin g a New Supplier</a>

    The alert pops up but it does not redirect

    What am i doing wrong???



  • Dietmar Meier

    #2
    Re: window.location not working

    Craig Keightley wrote:
    [color=blue]
    > the link is as follows:
    >
    > <a href="javascrip t:;" onClick="confir mNewSupplier()"
    > class="mainText Normal">Creatin g a New Supplier</a>
    >
    > The alert pops up but it does not redirect[/color]

    Some user agents do some optimization on links and do not change the
    location while a link is active, but cache the assignments to location
    properties instead. You can watch this in MSIE, while Gecko based
    browsers would do what you expected. After executing your function,
    the link is executed and "javascript :;" is assigned to the href
    property, overwriting what was assigned by your function. Simply
    return false in your click event handler to suppress the link's
    default action:

    <a href="#" onclick="confir mNewSupplier(); return false" ...>...</a>

    A better approach that would keep the document working in User Agents
    not using JS (or having JS disabled) would be:

    function confirmNewSuppl ier(){
    return confirm(
    ' Are you sure that you can\'t select a supplier from the '
    + 'list? \n(Click OK to request a new supplier or Cancel to '
    + 'return to the Directory)'
    );
    }
    ....
    <a href="l1stage1. php" onclick="return confirmNewSuppl ier()" ...>...</a>

    I would prefer another variant that separates code and text:

    var oMsg = {
    ...,
    "NewSupCnf" : "Are you sure that you can't select a supplier from the list?\n"
    + "(Click OK to request a new supplier or Cancel to return to the "
    + "Directory) ",
    ...
    }
    ....
    <a href="l1stage1. php" onclick="return confirm(oMsg.Ne wSupCnf)" ...>...</a>

    ciao, dhgm

    Comment

    • Craig Keightley

      #3
      Re: window.location not working

      thats great thank you


      "Dietmar Meier" <usereplytoinst ead@innoline-systemtechnik.d e> wrote in
      message news:39lcgkF63o tt0U1@individua l.net...[color=blue]
      > Craig Keightley wrote:
      >[color=green]
      >> the link is as follows:
      >>
      >> <a href="javascrip t:;" onClick="confir mNewSupplier()"
      >> class="mainText Normal">Creatin g a New Supplier</a>
      >>
      >> The alert pops up but it does not redirect[/color]
      >
      > Some user agents do some optimization on links and do not change the
      > location while a link is active, but cache the assignments to location
      > properties instead. You can watch this in MSIE, while Gecko based
      > browsers would do what you expected. After executing your function,
      > the link is executed and "javascript :;" is assigned to the href
      > property, overwriting what was assigned by your function. Simply
      > return false in your click event handler to suppress the link's
      > default action:
      >
      > <a href="#" onclick="confir mNewSupplier(); return false" ...>...</a>
      >
      > A better approach that would keep the document working in User Agents
      > not using JS (or having JS disabled) would be:
      >
      > function confirmNewSuppl ier(){
      > return confirm(
      > ' Are you sure that you can\'t select a supplier from the '
      > + 'list? \n(Click OK to request a new supplier or Cancel to '
      > + 'return to the Directory)'
      > );
      > }
      > ...
      > <a href="l1stage1. php" onclick="return confirmNewSuppl ier()" ...>...</a>
      >
      > I would prefer another variant that separates code and text:
      >
      > var oMsg = {
      > ...,
      > "NewSupCnf" : "Are you sure that you can't select a supplier from the
      > list?\n"
      > + "(Click OK to request a new supplier or Cancel to return to
      > the "
      > + "Directory) ",
      > ...
      > }
      > ...
      > <a href="l1stage1. php" onclick="return confirm(oMsg.Ne wSupCnf)"
      > ...>...</a>
      >
      > ciao, dhgm[/color]


      Comment

      Working...