Forum Moderators: open
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?
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
You could try delaying the execution of 'location.href' ...?
function test1() {
setTimeout("location.href='http://google.com'",500);
return false;
}
<!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>
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. ;)
<!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.
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.
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]
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).
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]
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.
<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]
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.