469,342 Members | 5,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

window.location not working

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

function confirmNewSupplier(){
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="javascript:;" onClick="confirmNewSupplier()"
class="mainTextNormal">Creating a New Supplier</a>

The alert pops up but it does not redirect

What am i doing wrong???

Jul 23 '05 #1
2 6273
Craig Keightley wrote:
the link is as follows:

<a href="javascript:;" onClick="confirmNewSupplier()"
class="mainTextNormal">Creating a New Supplier</a>

The alert pops up but it does not redirect


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="confirmNewSupplier(); 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 confirmNewSupplier(){
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 confirmNewSupplier()" ...>...</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.NewSupCnf)" ...>...</a>

ciao, dhgm
Jul 23 '05 #2
thats great thank you
"Dietmar Meier" <us***************@innoline-systemtechnik.de> wrote in
message news:39*************@individual.net...
Craig Keightley wrote:
the link is as follows:

<a href="javascript:;" onClick="confirmNewSupplier()"
class="mainTextNormal">Creating a New Supplier</a>

The alert pops up but it does not redirect


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="confirmNewSupplier(); 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 confirmNewSupplier(){
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 confirmNewSupplier()" ...>...</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.NewSupCnf)"
...>...</a>

ciao, dhgm

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Scott | last post: by
2 posts views Thread by venkatesh | last post: by
1 post views Thread by Tersia Ehlert | last post: by
2 posts views Thread by Dennis Ålund | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.