By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,773 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,773 IT Pros & Developers. It's quick & easy.

window.location not working

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.