homepage Welcome to WebmasterWorld Guest from 54.197.94.241
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
onclick="return false" no longer work under VISTA?
with "location.href=" in function
iProgram




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

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?

 

netfiends




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

I haven't tested in vista but: onclick="function(); return false" works for me on XP, maybe try that?

iProgram




msg:3210288
 10:05 am on Jan 6, 2007 (gmt 0)

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.

coopster




msg:3210597
 6:49 pm on Jan 6, 2007 (gmt 0)

Are you certain that the machine running Windows Vista with IE7 is setup to allow JavaScript?

iProgram




msg:3210963
 3:54 am on Jan 7, 2007 (gmt 0)

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

penders




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

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;
}

iProgram




msg:3211856
 7:13 am on Jan 8, 2007 (gmt 0)

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?

cmarshall




msg:3213393
 3:41 pm on Jan 9, 2007 (gmt 0)

I can't imagine that they would change this. I need to reinstall my Vista VM before I can test.

Fotiman




msg:3213565
 5:14 pm on Jan 9, 2007 (gmt 0)

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

cmarshall




msg:3213664
 6:21 pm on Jan 9, 2007 (gmt 0)

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

We've had the RTM since December.

iProgram




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

>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."

cmarshall




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

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>

iProgram




msg:3220792
 9:34 am on Jan 16, 2007 (gmt 0)

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)

cmarshall




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

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. ;)

Xuefer




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

same problem here
winxpsp2 ie7 with latest patch applied

cmarshall




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

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...

pageoneresults




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

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.

cmarshall




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

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.

Fotiman




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

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.

cmarshall




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

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.

Fotiman




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


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]

cmarshall




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

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?

Philosopher




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

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.

cmarshall




msg:3221314
 7:11 pm on Jan 16, 2007 (gmt 0)

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).

Rambo Tribble




msg:3221831
 4:38 am on Jan 17, 2007 (gmt 0)

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]

iProgram




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

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]

cmarshall




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved