Forum Moderators: open
OK, now I am a real noob at javascript, a real hacker so bear with me!
My employer encourages students to access the library's cgi search engine (search the library catalogue) from in school and at home. The problem is that they are two different links depending on if they access it from on site or not.
I think I can get around it by creating a javascript function that:
1. finds their IP adress,
2. with an if else statement I make ip's in the range of our intranet go to one link and others go to the external link.
My javascript knowlege comes from skim reading html goodies tutorial on it! (html goodies... aah is there anything you can't do?) :) But here's what I have so far.
var ip = '<!--#echo var="REMOTE_ADDR"-->';
function ip_else_if()
{if REMOTE_ADDR = 10.2.0.¦3.254)
{location.href="http://internallink.cgi"
}
else
location.href="http://externallink.cgi"
}
So how to I activate it when they click on the link on the website? Can I include all this as part of a onMouseClick=""? I have it in the header atm.
I would be so greatful of any help you could offer!
if REMOTE_ADDR = 10.2.0.¦3.254) should read
if (ip.indexOf('10.2.0.') > -1 ¦¦ ip.indexOf('3.254.') > -1)
Keep the script in the header (between <script> and </script>, off course).
The hyperlink:
<a href="#" onClick="ip_else_if(); return false"> click me </a>
...Why the '#' for the href? and (sorry to be a pain!)
Links don't work as links unless there is something in the href. # is good because it tricks the browser into thinking that there's a link to an anchor. In this case, the link goes nowhere but the onClick event does something instead.
..."¦¦" means range?
No, ¦¦ means or. It's a boolean thing, just like && means and.
..."> -1" why is that there?
If the string is not found, indexOf will return -1. If the string is found, indexOf will return the index number of the first char in the string. Indexes are zero-based - this means the first position is 0, the second position is 1, etc.
The anchor tag can be used without the href attribute and only the onclick attribute, though that prevents the tag's content from displaying as a link.
The accepted way to trigger a JavaScript function through a link is with the "javascript:" pseudo-URL, as in -
<a href="javascript:myFunction();"> Link text </a>
This avoids the need for an onclick attribute and allows the link to display with link attributes.
I am still having error messages on the hyperlink line: 'object expected' errors.
Do I need to do anything to link to SSI? I am just initialising the script with the standard
<script language="javascript></script> I don't need to add anything else do I?
(the net admin is still getting back to me about SSI, but I am pretty sure we have it installed) That could be the problem
That usually means that the function called does not exist. Did you check the names?
> Do I need to do anything to link to SSI?
No. If SSI is enabled, you should see your IP in the page source that the server returns. If you see the '<!--#echo var="REMOTE_ADDR"-->', SSI is not enabled.
> I am just initialising the script with the standard
<script language="javascript></script> I don't need to add anything else do I?
Browsers don't really care much, but the correct way would be
<script type="text/javascript">
var ip = '<!--#echo var="REMOTE_ADDR"-->';
function ip_else_if() {
...
}
</script>
The language attribute is deprecated as of HTML 4.0 (iirc).
I actually like using the href to hold what it should on scripts that in the end are going to result in a navigation effect - the actual href. ALA had a piece on that for accessible pop-ups recently, and I have always liked using attributes for as close to what they should be.
I am a bit curious as to why using a dummy href would be preferred over the pseudo-URL, since it would seem that chasing a non-link would waste processor cycles.