Forum Moderators: open

Message Too Old, No Replies

Intranet ip range vs linking

changing the location of a hyperlink depending on ip adress range

         

revolu7ion

2:24 am on Mar 24, 2004 (gmt 0)

10+ Year Member



hi,

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!

RonPK

6:45 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that the script relies on Server Side Includes. (JavaScript itself cannot detect the IP address).

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>

revolu7ion

1:54 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Cheers,

Rather than just copy it and learn nothing, can I ask about the code?

Why the '#' for the href? and (sorry to be a pain!)
"¦¦" means range? and (ok last one)
"> -1" why is that there?

If you tell me, I won't be such a noob and won't have to keep asking! :)

Purple Martin

2:17 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wish more people would make an effort to understand like you do!

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

Rambo Tribble

5:05 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps it should be noted that to include an href attribute/value and an onclick attribute/value in an anchor element is a bit nonstandard. Admittedly, it does manage to work, but it is giving the browser simultaneous, conflicting instructions.

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.

revolu7ion

5:36 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Thanks a lot for all that,

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

RonPK

7:42 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I am still having error messages on the hyperlink line: 'object expected' errors.

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

revolu7ion

1:54 am on Mar 26, 2004 (gmt 0)

10+ Year Member



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

OK, if that is the case then SSI is not enabled.

I'll complain loudly to the powers that be. Thanks for your help!

qeantk

6:34 am on Mar 26, 2004 (gmt 0)

10+ Year Member



Rambo: It's actually about as standard as it gets. There are no conflicting instructions. The onclick occurs and then the normal link behavior is processed - that is not a conflict. Returning false on the first prevents the second. Putting in a # (or a jvascript:void(null);, if you prefer) will give you a nice fallback for canceling if there is a script error on your event handler. As far as I understand this is actually the preferred way to do things - javascript hrefs are a bit of an ugly kludge.

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.

Rambo Tribble

2:22 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the clarification, geantk. It should, perhaps, be noted that the return value must be included in the value of the event handler, and cannot be set by a function called by the event handler. A global boolean, however, can be set by a called function and its value returned by a return statement integral to the handler's value.

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.

Rambo Tribble

11:18 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I realized, in retrospect, that my previous post was ill-conceived. Using the return statement prior to the function invocation does allow the function to directly return a boolean value to the event handler. The global variable is not necessary to pass the boolean value.