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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining current domain in Javascript 1

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

Following snippet will redirect to mysite.com but have a single website accessible also as mysite.net., mysite.org, etc.
Code:
<script language="javascript" type="text/javascript">
     <!--
     window.setTimeout('window.location="[URL unfurl="true"]http://www.mysite.com/index.html";[/URL] ',5000);
     // -->
 </script>
Would like to add a variable, say var this_domain, to which would be assigned the current domain, and then use
Code:
'window.location=this_domain+"/index.html"; '
or similar.

Uncertain as to how to assign the domain value to the variable?

TIA


FAQ184-2483​
Chris [pc2]
PDFcommander.com
motrac.co.uk
 
Something like this should do it:
Code:
<script language="javascript" type="text/javascript">
     <!--
     var myDomain = document.domain + '/index.php';
     window.setTimeout('window.location="[URL unfurl="true"]http://'[/URL] + myDomain + '"; ',5000);
     // -->
</script>

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Actually, you might even get away with this:
Code:
<script language="javascript" type="text/javascript">
     <!--
     var newPage = '/index.php';
     window.setTimeout('window.location="' + newPage + '"; ',5000);
     // -->
</script>

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sorry, for the numerous posts. These are the best (i.e. most accurate) of my examples...

The following ensures that any folder names in the original URL are retained by using the "." notation.
Code:
<script language="javascript" type="text/javascript">
     <!--
     var myDomain = './index.php';
     window.setTimeout('window.location="' + myDomain + '"; ',5000);
     // -->
</script>

The following is a more accurate way to retrieve the protocol, domain and path.
Code:
<script language="javascript" type="text/javascript">
     <!--
     var myDomain = location.protocol + '//' +
                    location.hostname +
                    location.pathname.substring(0, location.pathname.lastIndexOf('/')) +
                    '/index.php';
     window.setTimeout('window.location="' + myDomain + '"; ',5000);
     // -->
</script>

See for more info.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top