Forum Moderators: open

Message Too Old, No Replies

What are alternatives to javascript links using history?

w3c guidelines recommend against using javacript as link target

         

zollerwagner

9:54 am on Feb 25, 2005 (gmt 0)

10+ Year Member



I've been using javascript with the history property to go back to previous page after running multiple page php script-driven features. Are there good alternatives to this? Or is it just a matter of selecting a generic "go to" page for everyone?

The W3C [w3.org] says:


6.3 Ensure that pages are usable when scripts, applets, or other programmatic objects are turned off or not supported. If this is not possible, provide equivalent information on an alternative accessible page. [Priority 1]
For example, ensure that links that trigger scripts work when scripts are turned off or not supported (e.g., do not use "javascript:" as the link target). If it is not possible to make the page usable without scripts, provide a text equivalent with the NOSCRIPT element, or use a server-side script instead of a client-side script, or provide an alternative accessible page as per checkpoint 11.4. Refer also to guideline 1.
Techniques for checkpoint 6.3

Sure could use a usability/accessibility section here, couldn't we?

zollerwagner

7:48 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



Maybe it would be possible to use window.document.write() with the history element and thus avoid javascript as the target?

cmatcme

5:41 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



If you mean an alernative to history links not using javascript then:

<a href="history.go(-1)">

BlobFisk

5:44 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<a href="history.go(-1)">

This is still using JavaScript.... the history.go function is a JS one...

cmatcme

7:01 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



If there is a page where you are most likely to have come from when visiting that page, just link it to that.

*****

Eg:

To visit page2.asp, you are most likely to have to go through home.asp to get there, so do.

<script>
<!--
document.write("<a href=history.go(-1)>Back</a>");
//-->
</script>
<noscript>
<a href="home.asp">Back</a>
</noscript>

*****

That's what I would do but if that's not your scenario then just link to home.

zollerwagner

10:38 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Thanks to everyone for the assistance.

So far, I've not been able to get rid of the "javascript" in "javascript:history.go(-1)". How do you get the link to work without it?

This return link appears after a form is successfully filled in. There may be several attempts before the process is successful, so I have a counter to keep track of the number of times the processing page is redrawn.

This code appears at the top of the page in php:


// define return link options for success page
$returnlink = "<script type='text/javascript'>
//<![CDATA[
window.document.write('<p>Return to <a href=\"javascript:history.go(" . $_POST['historyCounter'] . ");\">last page visited<\/a><\/p>');
//]]>
</script>
<noscript><p>Return to <a href='/'>Home</a></p></noscript>";

Then in my page when I want to show the link I just put


<?php
echo $returnlink;
?>

It works and w3.org's html validator accepts it. The W3 .org's link validator [validator.w3.org] almost goes for it. Here's what it reports:


javascript:history.go(-1);
What to do: You must change this link: people using a browser without JavaScript support will not be able to follow this link. See the Web Content Accessibility Guidelines on the use of scripting on the Web and the techniques on how to solve this.
Response status code: 501
Response message: Protocol scheme 'javascript' is not supported

(and on the 501 status code: "Could not check this link: method not implemented or scheme not supported")

Since I've given a <noscript> option, I'm guessing that I'm okay using javascript:history.go(-1);

Does that make sense to you all?

topr8

10:45 am on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



with my asp sites (but the principle is the same) when using pages to run a script and then pass through to another page, i pass the url of the original page along with the other stuff (either as a cookie value, session variable or as a querystring parameter) and construct a back link using that.

of course if cookies are off then it doesn't work using cookies but then they need cookies enabled to use my sites anyway.

zollerwagner

6:01 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Thanks for the idea.

So far, I've only got one page that needs php in this site, so it struck me as overkill to make every page go through php interpretation just to remember where the user was. But maybe not.

Are there other things you do with asp/php on what seem like otherwise normal pages (not needing asp/php coding)?

encyclo

6:47 pm on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the solution in message #6 is far too complicated for general use, and it appears to be more aimed at squeezing through validation than being useful.

The problem with

javascript:
links is that they do nothing if Javascript is disabled. To be compliant, all you need to do is to provide a straight href link overridden by the Javascript if available:

<a href="home.asp" [b]onclick="history.go(-1);return false;"[/b]>

zollerwagner

7:41 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



You're right. What I wanted was a way to have a back up in case JavaScript wasn't enabled.

Yours would be a much simpler solution and it gets rid of the "javascript:" part. The validators like it, and don't even seem to notice the "onclick".

I'd use it, except that labeling the link becomes very difficult. (I'm open to suggestions!) I don't want to say "return to previous page" because it might go to the home page. Unless there's a way to label the link accurately it becomes a "Go somewhere" link.