본문 바로가기

스크립트/자바스크립트

JavaScript Window.Location

JavaScript Window.Location

Control over what page is loaded into the browser rests in the JavaScript property window.location. By setting window.location equal to a new URL, you will in turn change the current webpage to the one that is specified. If you wanted to redirect all your visitors to www.google.com when they arrived at your site, you would just need the script below:

HTML & JavaScript Code:

<script type="text/javascript">

<!--

window.location = "http://www.google.com/"

//-->

</script>
http://www.tizag.com/javascriptT/javascriptredirect.php 
--> 이 경우, 지원하지 않습니다. 라는 에러가 발생하면서 되지 않는다 (IE, safari, firefox)
 
window.location 과 비슷한 기능을하는 객체로

window.location 는 창의 위치를
document.location 는 페이지의 위치를 나타낸다.

프레임이 나눠져있는 경우라도
window.location 프레인 내의 페이지를 가지고 있는 창의위치를 나타내기에 실제
documenr.location 과 결과물은 동일하다.

다르게쓰이는 경우도 있을려나.. 내는 아직 몰것네..

ps. 위치, 위치.. 하니까 좀 어렵게 느껴진다면.....
그냥 해당 웹페이지의 http:// 에서 부터 파일명을 포함한 주소를 나타낸다고 하면 좀 쉬울려나.


tip. location 객체에는 href 라는 속성을 가지며
reload(), replace() 라는 메소드를 가지는데 차이가 .. (..

★★★★★
window.location.href = '주소'; //주소로 이동
window.location.replace('주소');  //주소로 이동
window.location.reload(); 새로고침

★★★★★
replace()의 경우 history를 남기지 않기때문에 뒤로가기를 할수없음.
 

<meta http-equiv="refresh" content="지연시간"; url=이동할 페이지">
ex.
<mata http-equiv="refresh" content="10"; url=http://www.naver.com">


<script language="javascript">
//
자바 스크립트를 이용한
location
//window.open()
이용시 새창으로 가능

//location.href="http://bombfox.egloos.com";
window.open("
http://www.naver.com");
</script>



Note: Be careful using this if the page you use it on is listed in a search engine. Many of them find redirection to be a way of attempting to spam their index, and may remove the site from the listings. If you are unsure, don't try this-- or contact the search engine for their rules.

Redirection is often used to take viewers to a page depending on their browser's name or version. To redirect a viewer instantly, you just need to add a short command in your head section:

<HEAD>

<SCRIPT language="JavaScript">

<!--

window.location="http://someplace.com";

//-->

</SCRIPT>

</HEAD>

This would take the viewer right to the url you used as soon as they start loading the page. Sometimes this is done if a site has moved to another location. Another use for it is to detect a browser type and redirect your viewers to one page for Netscape, another of Internet Explorer, or a third for other browsers:

<HEAD>

<SCRIPT language="JavaScript">

<!--

var browserName=navigator.appName;

if (browserName=="Netscape")

{

window.location="http://www.someplace.com/ns.html";

}

else

{

if (browserName=="Microsoft Internet Explorer")

{

window.location="http://www.someplace.com/ie.html";

}

else

{

window.location="http://www.someplace.com/other.html";

}

}

//-->

</SCRIPT>

</HEAD>

This uses the browser detection method from the previous section. Rather than popping up an alert box, we redirect the viewer to a page that best suits the browser being used.

If you want to have one page for version 4 browsers and another for others, we can take another script from the previous section and change it in the same way:

<HEAD>

<SCRIPT language="JavaScript">

<!--

var browserVer=parseInt(navigator.appVersion);

if (browserVer >= 4)

{

window.location="http://www.someplace.com/v4.html";

}

else

{

window.location="http://www.someplace.com/other.html";

}

//-->

</SCRIPT>

</HEAD>

Not too bad, the only trouble is the need to create so many pages!

You can also use this to help people who come into your site, but come in on a page that should be within a frameset that you use for navigation. Using the top.frames.length object, you can find out if the page is in a frame or not. If the value is zero, the page is not in a frame. If it is greater than zero, the page is inside a frame. So, you could take the viewer back to your main frameset page using a script like this:

<HEAD>

<SCRIPT language="JavaScript">

<!--

function getgoing()

{

top.location="http://someplace.com";

}

if (top.frames.length==0)

{

alert("You will be redirected to our main page in 10 seconds!");

setTimeout('getgoing()',10000);

}

//-->

</SCRIPT>

</HEAD>

This will alert the viewer and take them to the main page after 10 seconds. You can change the number in the setTimeout function to adjust the time if you like.

You can also use it to break a viewer out of someone else's frames if they followed a link that didn't have the target set correctly. It uses the same idea in reverse:

<HEAD>

<SCRIPT language="JavaScript">

<!--

function getgoing()

{

top.location="http://someplace.com";

}

if (top.frames.length > 0)

{

alert("The frames will be broken in ten seconds.");

setTimeout('getgoing()',10000);

}

//-->

</SCRIPT>

</HEAD>

As you can see, redirection can be a handy tool at times. Just use it with caution if you are worried about the search engines.

Well, that does it for now. Let's go check out the next section, New Windows with JavaScript.  

 

http://www.pageresource.com/jscript/jredir.htm