2 posts tagged “javascript”
Well, since my journal is already a bitch fest...:)
I've been constructing a site. I decided to use the <base href="..."> to _try_ simplify things in my documents. This is fine and dandy, except for named hrefs (i.e. links to items like #top).
When using base, named hrefs will only point to named refs on the base page. I decided to try and solve things with javascript. Here's what i came up with:
code.js:
function namedRef(strLink, strText) {var intPosition = document.location.href.indexOf("#");if (intPosition < 0) {intPosition = document.location.href.length;}return "<a href=\"" + document.location.href.substring(0, intPosition) + "#" + strLink + "\">" + strText + "<" + "/a>";}
somewhere in the html file:
<script type="text/javascript">document.write(namedRef('top', 'Top'));</script>
This is great! It'll give me things like test.html#top and if you've already clicked on a named href, it won't duplicate it. It even works in Netscape 4. It bungholios things for lynx though.
...There's one tine exception to things working in Netscape 4. It won't do links that end in a slash (that may not be the EXACT condition, but for what i know, it works on site/index.html but not site/)
Perhaps i should forget using the base tag. It seems like it's more trouble than it's worth.
We have a batch of ASPs at work that we affectionately (well, not really) call the "survey generator." They were created by a co-op student a while back, before I even started working here. I've been lucky enough to have avoided it -- until now.
I was trying to apply some simple javascript form validation to it, but for some reason, it kept throwing an error. So, I re-read my references to make sure I was doing this properly. All seemed well.
It turns out, that you can't have a tilde in the name of a form element. Now, I'm not sure who's brilliant idea it was to use tildes in the first place, but it sure puts a wrinkle in my plans. Here's some sample code to show you what's up:
<html><head><title>Tilde Test</title><script language="javascript">function checker() {/** This next line generates an error due to the* tilde in the name. Evil.** Line: 15* Char: 20* Error: Expected ')'*/// if (document.testform.tilde~tilde.checked) {if (document.testform.elements[0].checked) {alert('checked!');}else {alert('unchecked!');}return false;}</script></head><body><form name="testform"><p><input type="checkbox" name="tilde~tilde"/> Check Me</p><p><input type="submit" onclick="return checker();"/></p></form></body></html>
So, now everything has to be referenced by element number (elements[x]) instead of by name. But, with names like 2~txt~3, is it really any worse?
Update: Hey... vsergu tells me that
if (document.testform['tilde~tilde'].checked) {
should work. A quick test shows that it does. Thanks! Why don't people use this method? Everywhere i read it's either formname.elementname or forms[x].elements[y].