...

View Full Version : "javascript:" links, submitting forms, and "open in new window"



bluemorocco
05-04-2007, 10:06 PM
Disclaimer: I would love to refactor things so that we were not using "javascript:" links but that's what we inherited and we may not have time to (yet, at least).

Anyway - from the FAQ:
http://codingforums.com/showthread.php?p=255428#post255428

Q: How do I use 'javascript:' links?
A: Consensus on this area among professional web designers is that they shouldn't be used at all. There are strong arguments arguments towards this, from usability, accessibility, optimisation and standards comformance perspectives. Before I tell you how to use them, please read through this list first...

* Usability:
Something not often mentioned but one of the most important
factors in my view, is that the 'javascript:' pseudo-URI destroys
several layers of usability features. E.g:
- The link does in most cases only work when simply clicked, it
generally no longer works:
* When bookmarked
* When users chose 'open in new window' or 'open in new tab'

So - is this pretty much a fact of life with javascript: links?

We have a page that looks something like this (example simplified for ease of understanding):



<html><body>

<form id="recordDetailForm" action="details.jsp" id="theDetailForm">
<input type="hidden" name="detailNumber" />
</form>

<script language="javascript">
function goToDetail(_detailNumber) {
var theForm = document.getElementById('theDetailForm');
theForm.detailNumber.value = _detailNumber;
theForm.submit();
}
</script>

<a href="javascript:goToDetail('35');">open record 35</a>
</body></html>
This works fine if someone clicks the link normally, but if they right click the link and select "open in new window" they get "the page cannot be displayed" in IE. Firefox doesn't even offer "open in new tab".

You may be thinking, "hey, why don't you just generate the URL detail.jsp?detailNumber=35"? The unfortunate/annoying thing is that the URL's are generated by the software we're using and we are still trying to figure out how to generate the right URL without using a form. (It's a long, sad story - just trust me, we wouldn't do it this way voluntarily)

I saw an example that said to try this:




<html><body>

<form id="recordDetailForm" action="details.jsp" id="theDetailForm">
<input type="hidden" name="detailNumber" />
</form>

<script language="javascript">
function goToDetail(_detailNumber) {
var theForm = document.getElementById('theDetailForm');
theForm.detailNumber.value = _detailNumber;
theForm.submit();
return false;
}
</script>

<a href="#" onclick="goToDetail('35');">open record 35</a>
</body></html>
That didn't *really* help. It now just re-shows the first page when we do "open in new window" instead of showing "page cannot be displayed".

So - outside of just generating normal HTTP GET links - any ideas?

thanks,
john

Vladdy
05-04-2007, 10:22 PM
Is ability of the page to open in a new window a requirement, or you are trying to prevent an error if someone attempts to do it?

glenngv
05-05-2007, 12:24 AM
Is using a button an option?

<input type="button" value="open record 35" onclick="goToDetail('35');" />

bluemorocco
05-07-2007, 05:04 PM
Is ability of the page to open in a new window a requirement, or you are trying to prevent an error if someone attempts to do it?It's a "nice to have". Our target environment is actually IE 6, but with IE 7 and Firefox making tabs more popular, it would be swell if we could give people the option.

We can prevent the error with the "return false" thing described at the bottom of the post, though all that does is take the user back to the page they're on.


Is using a button an option?

<input type="button" value="open record 35" onclick="goToDetail('35');" />I don't think so. I believe that for purely asthetic reasons they don't want buttons in their tables :)

edit: oh yeah. You can't open a button click in a new window :(

thanks,
john



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum