Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange behavior of length function in IE

Status
Not open for further replies.

FaneDuru

Technical User
Jul 15, 2002
141
RO
I have a text box keeping the IP of the visitor.
I obtain IP in a text box ('ipaddr') of a form named 'myform' using the next script:
Code:
var ip = '<!--#echo var="REMOTE_ADDR"-->'

function ipval() {
document.myform.ipaddr.value='Your IP: '+ip;
}
I would like to detect if the file is opened from local computer or from my domain trying to measure the length of the text in this text box. I must adapt the code because the company hosting the domain is adding a banner at top of tha page and I need to position some tags according to that... It works in FireFox but not in IE:
Code:
 var sT =document.myform.ipaddr.value;
 if (sT.length = 41){
    alert('local')
    //do something...
    }else {
    alert('hosted')
    //do something else...
 }
Length of the string is always 0 in IE...
Can you help on that issue ?
Thanks in advance!
Fane Duru
 
Code:
if (sT.length = 41){

isn't a single = sign an assignment operator in javascript? i thought it was like php and used == as a comparison operator.
 
I used:
Code:
if (sT.length == 41){
I have just wrongly written in the posted code...
I am afraid that the problem is connected with the way of reading scripts for the two browsers. I have used some alerts and in the moment of alert in the text box of IE it is really nothing. In Firefox is already the IP...
 
Is it another simple way to identify if the file is on the local computer or is hosted ?

Thanks,
Fane Duru'
 
caveat: i'm no javascript expert...

are you definitely parsing (on your local and remote machines) your html for SSI?

to determine the remote ip address, have you tried using a java method instead? or aswell?

and why are you referencing a form control rather than the ip variable itself?

Code:
var ip = '<!--#echo var="REMOTE_ADDR"-->';
document.onload = function(){
  if (ip.length > 15) { //not an IP address
   if (java && java.net){
    ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
   } else {
    ip = 'unknown';
  }
 }
}

as to whether the file is remote or local, how about just testing the location.href to see whether it matches your remote domain?

Code:
var myDomain = '[URL unfurl="true"]http://www.domain.com';[/URL] //hard code
function isRemote(){
  var l = (location.href).toLowerCase();
  if (l.indexOf(myDomain) === -1) return false;
  return true;
}
 
Hi

jpadie said:
caveat: i'm no javascript expert...
Code:
[red]document[/red].onload = function(){
That explains it. Should be [tt]window.onload[/tt]. ;-)

Anyway, if you want to know if the request came from the local network or from outside, then why not pass that information to JavaScript instead of the remote address ?
Code:
var islocal=<!--#if expr="$REMOTE_ADDR = /^(192|127)\..*/" -->true<!--#else -->false<!--#endif -->
Tested with Apache.


Feherke.
 
Thanks jpadie,
I am not a javascript expert, too. I think it is obvious... You are right in both cases. I was looking for something like your isRemote() function. It happened I had such a form keeping visitor IP on my page and I thought 'I am clever' using it to determine the host...
Now I could build the function based on my previous thought and your idea to use the variable:
Code:
ip = '<!--#echo var="REMOTE_ADDR"-->';// it has been declared prior to that...
function isRemote(){
  if (ip.length > 15) { //not an IP address
     return false;
  } else {
     return true;
  }
}
Your code:
Code:
 ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
returns local IP 127.0.0.1 in both cases using Firefox and returns nothing in IE and Google Chrome.
Anyhow your second suggestion is the one I will use.

Thanks again,
Fane Duru'
 
Hi feherke,

I am afraid I am not able to understand your suggestion... Is that javascript...? I have tested it with IE, Firefox and Chrome and all of them returns 'undetermined' for the variable...
Is it something which I miss?

I did not use document.onload because at <body onload...> I am calling another function.

Fane Duru'
 
feherke's suggestion is a conditional server side include.

@feherke
as you have already pointed out elsewhere, i'm not myself today!
 
Hi

Fane Duru' said:
Is that javascript...?
It is similar to you initial code :
Code:
var ip = '<!--#echo var="REMOTE_ADDR"-->'
Code:
var ip = '127.0.0.1'
My code sets the islocal JavaScript variable to [tt]true[/tt] for local requests ( remote IP address starting with 192 or 127 in my example ) :
Code:
var islocal=<!--#if expr="$REMOTE_ADDR = /^(192|127)\..*/" -->true<!--#else -->false<!--#endif -->
Code:
var islocal=true
As I mentioned, the suggested SSI was tested with Apache. In case you use other web server, the interpretation of SSI directives may vary.

Feherke.
 
Is it (sic) another simple way to identify if the file is on the local computer or is hosted ?

If locally you are using the "file" protocol, then yes:

Code:
alert(location.protocol);

If you are running from a web server locally, then this will obviously not work.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks Dan,

I have also used:
Code:
if(location.protocol == 'file:'){alert('local');}else if(location.protocol== 'http:'){alert('remote');}
and worked very well as you suggested.

Fane Duru'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top