Welcome to WebmasterWorld Guest from 54.166.191.159

Forum Moderators: open

Message Too Old, No Replies

onclick="return false" no longer work under VISTA?

with "location.href=" in function

     
3:52 am on Jan 6, 2007 (gmt 0)

10+ Year Member



Take a look of the following code:

HTML:

<a href="page1.html" onclick="return test()">Test</a>

JS:

function test()
{
location.href = 'http://example.tld';
return false;
}

It works very well under Windows XP (IE7/Opera/Firefox) and under Windows Vista (Opera/Firefox). When a user clicks "Test", he will visit http://example.tld, not page1.html.

However the above code does not work under Windows Vista with IE7. How to fix this issue?

7:10 am on Jan 6, 2007 (gmt 0)

10+ Year Member



I haven't tested in vista but: onclick="function(); return false" works for me on XP, maybe try that?
10:05 am on Jan 6, 2007 (gmt 0)

10+ Year Member



I'm not sure but I have to use location.href in function. So it's not a good idea to move return false from function.
6:49 pm on Jan 6, 2007 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Are you certain that the machine running Windows Vista with IE7 is setup to allow JavaScript?
3:54 am on Jan 7, 2007 (gmt 0)

10+ Year Member



Yes of cause I have js enabled. I received this bug report from my customer.

If you have Windows Vista installed, try this by yourself:

<html>
<head>
<script type="text/javascript">
function test1()
{
location.href='http://google.com';
return false;
}
function test2()
{
alert('No Problem');
return false;
}
</script>
</head>
<body>
<p><a href="http://yahoo.com" onclick="return test1()">Test 1</a></p>
<p><a href="http://yahoo.com" onclick="return test2()">Test 2</a></p>
</body>
</html>

Test 1
All browsers under XP: google
Firefox, Opera under Vista: google
IE under Vista: yahoo

Test 2
All browsers under XP & Vista: No problem

6:32 pm on Jan 7, 2007 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I have not tried this under Vista either, but it seems 'location.href' is halting the execution of your script ('return false' is not being executed) - which in some ways is logical, since setting location.href should cause the browser to jump to another page; immediately?! ...but is doesn't!

You could try delaying the execution of 'location.href' ...?

function test1() { 
setTimeout("location.href='http://google.com'",500);
return false;
}
7:13 am on Jan 8, 2007 (gmt 0)

10+ Year Member



The problem is I use this method since many years ago and now I have to change these functions for all my websites, just for Vista & IE7?
3:41 pm on Jan 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't imagine that they would change this. I need to reinstall my Vista VM before I can test.
5:14 pm on Jan 9, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Are you running a Beta version of Vista? It doesn't come out until the end of this month (that's why I ask).
6:21 pm on Jan 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We're a software development shop, and we write Vista plugins.

We've had the RTM since December.

1:53 pm on Jan 10, 2007 (gmt 0)

10+ Year Member



>Are you running a Beta version of Vista? It doesn't come out until the end of this month (that's why I ask).

I use RC1 version but I just confirmed with my customer for this issue. He is using the MSDN Vista. "So it is retail, build 6000."

11:33 am on Jan 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This runs fine in Vista RTM/IE7:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
// <![CDATA[
function test()
{
location.href = 'http://www.webmasterworld.com/';
return false;
}
// ]]>
</script>
</head>
<body>
<a href="page1.html" onclick="return test()">Test</a>
</body>
</html>
9:34 am on Jan 16, 2007 (gmt 0)

10+ Year Member



cmarshall, I uploaded your code and it doesn't work for me. I'm using RC1 with IE7.

And then I sent the test page to my customer and he got the same result - does not work with Vista/IE7. My customer uses Vista Version 6.0.6000 (command> ver). IE 7.0.6000.16386 (256bit)

11:37 am on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmarshall, I uploaded your code and it doesn't work for me. I'm using RC1 with IE7.

And then I sent the test page to my customer and he got the same result - does not work with Vista/IE7. My customer uses Vista Version 6.0.6000 (command> ver). IE 7.0.6000.16386 (256bit)

I just tried it again to insure I wasn't crazy.

I use Vista RTM (Release to Manufacturers), which IS the shipping Vista. I'm running the "Business" installation, and it has been activated. I'm running on a 2.33GHz Core 2 Duo laptop, and Vista has 1500MB of RAM.

The code is executed on this machine locally, by a LAMP server setup (the machine is a Mac, and Vista is running in a Parallels VM).

It shows only the word "Test," highlighted as a link. Clicking on that link takes me to WebMasterWorld.

Maybe one issue might be that you are running on a remote server. I turned off all the anti-phishing stuff, so that may have something to do with it. If you want, PM me the external link, and I can hit it.

Sadly, the fact this works does not ensure that I am not crazy. I think it may be too late for that. ;)

11:58 am on Jan 16, 2007 (gmt 0)

10+ Year Member



same problem here
winxpsp2 ie7 with latest patch applied
12:12 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



same problem here
winxpsp2 ie7 with latest patch applied

AHA!

I suspect that it may have to do with IE7's anti-phishing filters. I'll try it out later today. Gotta run...

12:13 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member pageoneresults is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Just out of curiosity, if you change onclick to onkeypress, what happens?

Disclaimer: This is totally out of my realm. But, onclick has an equivalent from the usability side and that is onkeypress. I'm not sure if it even applies here.

12:50 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well the way that I would do it would be to not assign an onclick to an <a>. I would do something more like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>
test
</title>
<script type="text/javascript">
// <![CDATA[
function test()
{
location.assign ('http://www.webmasterworld.com/');
}
// ]]>
</script>
</head>
<body>
<a href="javascript:test()">
Test
</a>
</body>
</html>

However, I suspect that there is a particular reason for their choice. It is very specific.

5:23 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Ack! Holy lack of separation Batman!

In my opinion, using the onclick event handler is definately a better approach, as you can avoid mixing content with behavior that way. Also, there is no such "javascript" psuedo-protocol defined (even though this works in most browsers).

Just my 2 cents.

5:36 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, there is no such "javascript" psuedo-protocol defined (even though this works in most browsers).

I didn't know that. I've been using it (not that often, because it does cause issues) for years on sites that work on browsers up and down the scale. I've never had a single instance ever fail, and I write sites that need to support some pretty low expectations.

I have had issues with the data URI Scheme [tools.ietf.org]. That won't work on a lot of browsers.

Like I've said before, I'm not much of a Kool-Aid fan. I'll use whatever tool does the job, and that always upsets someone at some time.

In any case, why use an <a> if you will subvert it anyway? Why not simply use a <div> or a <input>? If all I wanted to do is give a placeholder for an onclick() I wouldn't use an <a> for it.

6:51 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month




In any case, why use an <a> if you will subvert it anyway?

For degredation. This is, after all, a link to another page. It's just being enhanced to open differently if JavaScript is available. And if not, it should fallback to the default.


Why not simply use a <div> or a <input>?

Because semantically, this is a link.


If all I wanted to do is give a placeholder for an onclick() I wouldn't use an <a> for it.

But that's not just a placeholder. It should still be accessible without the JavaScript.

[edited by: Fotiman at 6:52 pm (utc) on Jan. 16, 2007]

7:02 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it.

However, if we want accessibility, as someone pointed out, we shouldn't be using onclick(). Is there an event we can trigger for a link being triggered?

7:07 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The current setup already is accessible. With JS enabled, the onclick is triggered. Without JS enabled, the regular link is triggered. All visitors regardless of JS support will be able to use the link.
7:11 pm on Jan 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, this reminds me of a site I did once that had a "Page x of n" functionality, where you had a row of links along the bottom to select pages.

I remember that it was complete pain to make it work correctly, as we needed to POST to each page.

What we did was make each link a <form>, and the visible link was

<a href="javascript:document.getElementById('the_form').submit()">Page X</a><noscript> <input type="submit" value="Go"></noscript>
. It was messy looking if you hit it with JavaScript off, but it worked (and still does on a site that supports NS 4.X and up).
4:38 am on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While the javascript: pseudo-URL may not be part of a codified standard, it has been a browser convention since JavaScript's inception. Like the back button, there's no standards body that says a browser must have one, but you'd be hard pressed to find browser that doesn't support it. (Granted, browsers that don't support JavaScript won't implement the pseudo-URL and Lynx, being non-graphical, uses the cursor control keys for the functionality of forward and back.)

As an addendum, IE 7 is turning out to be as broken as one might expect, given the source. Word has it, it simply won't display anything on some newer ThinkPads.

[edited by: Rambo_Tribble at 4:41 am (utc) on Jan. 17, 2007]

9:13 am on Jan 17, 2007 (gmt 0)

10+ Year Member



The current setup already is accessible. With JS enabled, the onclick is triggered. Without JS enabled, the regular link is triggered. All visitors regardless of JS support will be able to use the link.

This is the point. I use this to make sure that my customers can use my Ajax member area.

<a href="/how-to-enable-your-javascript.html" onclick="return goto_ajax_member_area()">Visit Member Area</a>

But now my member area no longer works with Vista/IE7!

Maybe one issue might be that you are running on a remote server. I turned off all the anti-phishing stuff, so that may have something to do with it. If you want, PM me the external link, and I can hit it.

If you test the code in a local file, you will see warning message in IE7. I turned off the anti-phishing and tried the code with my two websites - not works. cmarshall you may check your PM box for URL of test page .

[edited by: iProgram at 9:36 am (utc) on Jan. 17, 2007]

11:30 am on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmarshall you may check your PM box for URL of test page

You're gonna hate me for this...

It works smooth as glass. I click on it and get sent right to Google (not Yahoo). No script errors or warnings. I do have Script Debugging and Errors turned on.

I noticed that you are from China. Are you using a localized version of Vista?

I'm concerned, because, if mine is working fine, and everyone and their mother is having this problem, then my test setup is not such a good test setup. So far, in my experience, it has been five-by-five with everyone else.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month