Solved

window.location.port not working?

Posted on 2004-09-07
11
291 Views
Last Modified: 2012-05-05
If I have the following in the HTML page thispage.htm
<body>
 <script language=Javascript>
 alert(window.location.hostname); //or (use .host)
 alert(window.location.port);
 </script>
</body>

and I go to http://222.222.222.222:80/thispage.htm

first alert I get is fine
222.222.222.222
second one comes up blank. Why?

Is it some sort of a firewall config.  

Can someone else share a snippet that actually gets the :80 (or any other) port number into a variable?

Thanks
0
Comment
Question by:geoenvi
  • 6
  • 5
11 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 12001624
If your window.location.href does NOT contain the port, then you will not get the location.port
Have a look at the complete location.href


0
 

Author Comment

by:geoenvi
ID: 12001642
I thought the location object contains what is typed in the URL addressbar of IE?  I browse to  http://222.222.222.222:80/thispage.htm and it clearly contains port number there
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12001651
I just tested on my IIS. You are right, it is strange. The port was not passed neither to href nor to port!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:geoenvi
ID: 12001659
My point exactly.  So.. what is the solution

:0)
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 12001678
Ok, I changed my IIS port from 80 to 8081 and the port was passed to href and port.
So my assumption is that  the port is normalized in any way before passed to script environment.

0
 

Author Comment

by:geoenvi
ID: 12001687
meaning that any port other than 80 would be passed into the location.port?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12001690
Yeap!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12001698
Or any port other then 443 for https.
0
 

Author Comment

by:geoenvi
ID: 12001712
Do you know if this is an IIS specific trait or is it IE specific or what?
0
 

Author Comment

by:geoenvi
ID: 12001731
Thanks Zvonko. You have helped a lot in the recent past. You can truly wear your your expert badge of honor

:-)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12001747
Thanks :-)

This worked also for port 80 on ASP side:
<% Response.Write("Port: " & Request.ServerVariables("SERVER_PORT")) %>

And I do not know how general that port problem is.
I can imagine that the normlization is done on browser side, because there is the scripting engine environment.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 22
Make options hidden and show just option that is on 1 29
key press alert 2 32
jquery datable hidden section doesn't submit 2 15
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question